Skip to content

Commit

Permalink
reformat, add source group.
Browse files Browse the repository at this point in the history
  • Loading branch information
yobeonline committed Oct 6, 2024
1 parent ffa30b8 commit 7a54f78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 deletions.
48 changes: 10 additions & 38 deletions io1_add_target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -227,34 +227,16 @@ function(fetch_source_files out_sources)
PARENT_SCOPE)
endfunction()


function(apply_source_files_properties)
foreach(str IN LISTS ARGN)
is_source_group("${str}" out)
if(out)
continue()
endif()

parse_file_options("${str}" file options)

cmake_parse_arguments(io1 "cpp;header" "" "" ${options})
if(io1_cpp)
set_source_files_properties("${file}" PROPERTIES LANGUAGE CXX)
endif()
if(io1_header)
set_source_files_properties("${file}" PROPERTIES HEADER_FILE_ONLY ON)
endif()
endforeach()
endfunction()

# adds src to target, applying options
function(io1_add_source_file target file props)
# adds src to target, applying options and source group
function(io1_add_source_file target file props group)
get_target_property(type ${target} TYPE)
if (${type} STREQUAL "INTERFACE_LIBRARY")
target_sources(${target} INTERFACE ${file})
else()
target_sources(${target} PRIVATE ${file})
endif()

source_group(NAME "${group}" FILES "${file}")

cmake_parse_arguments(io1 "cpp;header" "" "" ${props})
if(io1_header)
Expand All @@ -275,34 +257,24 @@ function(io1_is_source_group str res)
endfunction()

# expects "file.c[:opt1,opt2,opt3,...]" and parse it into "file.c" and the list
# "opt1;opt2;opt3"
# "opt1 opt2 opt3"
function(io1_parse_file_options str out_file out_options)
if("${str}" MATCHES "^(.*):(.*)$")
if ("${CMAKE_MATCH_1}" STREQUAL "")
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION}: ignored orphaned source option '${CMAKE_MATCH_2}'.")
else()
set(${out_file}
"${CMAKE_MATCH_1}"
PARENT_SCOPE)
set(${out_file} "${CMAKE_MATCH_1}" PARENT_SCOPE)

if("${CMAKE_MATCH_2}" STREQUAL "")
unset(${out_options}
PARENT_SCOPE)
unset(${out_options} PARENT_SCOPE)
else()
string(REPLACE "," ";" temp_out_options "${CMAKE_MATCH_2}")
set(${out_options}
"${temp_out_options}"
PARENT_SCOPE)
set(${out_options} "${temp_out_options}" PARENT_SCOPE)
endif()
endif()
else()
set(${out_file}
"${str}"
PARENT_SCOPE
)
unset(${out_options}
PARENT_SCOPE
)
set(${out_file} "${str}" PARENT_SCOPE)
unset(${out_options} PARENT_SCOPE)
endif()
endfunction()

10 changes: 5 additions & 5 deletions test/add_source_file.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function(${CMAKETEST_TEST})

project(foo LANGUAGES CXX)
add_library(foo STATIC)
io1_add_source_file(foo "foo/main.cpp" "")
io1_add_source_file(foo "foo/main.cpp" "" "")

get_target_property(prop foo SOURCES)
ct_assert_equal(prop "foo/main.cpp") # main.cpp was added to sources
Expand All @@ -25,7 +25,7 @@ function(${CMAKETEST_TEST})

project(foo_header LANGUAGES CXX)
add_library(foo_header STATIC "foo_header/main.cpp")
io1_add_source_file(foo_header "foo_header/header.cpp" "header")
io1_add_source_file(foo_header "foo_header/header.cpp" "header" "")

get_target_property(prop foo_header SOURCES)
ct_assert_equal(prop "foo_header/main.cpp;foo_header/header.cpp") # header.cpp was added to sources
Expand All @@ -43,7 +43,7 @@ function(${CMAKETEST_TEST})

project(foo_cpp LANGUAGES CXX)
add_library(foo_cpp STATIC)
io1_add_source_file(foo_cpp "foo_cpp/main.c" "cpp")
io1_add_source_file(foo_cpp "foo_cpp/main.c" "cpp" "")

get_target_property(prop foo_cpp SOURCES)
ct_assert_equal(prop "foo_cpp/main.c") # main.c was added to sources
Expand All @@ -62,7 +62,7 @@ function(${CMAKETEST_TEST})

project(foo_cpp_header LANGUAGES CXX)
add_library(foo_cpp_header STATIC foo_cpp_header/main.cpp)
io1_add_source_file(foo_cpp_header "foo_cpp_header/header.c" "cpp;header")
io1_add_source_file(foo_cpp_header "foo_cpp_header/header.c" "cpp;header" "")

get_target_property(prop foo_cpp_header SOURCES)
ct_assert_equal(prop "foo_cpp_header/main.cpp;foo_cpp_header/header.c") # header.c was added to sources
Expand All @@ -81,7 +81,7 @@ function(${CMAKETEST_TEST})

project(foo_header_cpp LANGUAGES CXX)
add_library(foo_header_cpp STATIC foo_header_cpp/main.cpp)
io1_add_source_file(foo_header_cpp "foo_header_cpp/header.c" "header;cpp")
io1_add_source_file(foo_header_cpp "foo_header_cpp/header.c" "header;cpp" "")

get_target_property(prop foo_header_cpp SOURCES)
ct_assert_equal(prop "foo_header_cpp/main.cpp;foo_header_cpp/header.c") # header.c was added to sources
Expand Down

0 comments on commit 7a54f78

Please sign in to comment.