Skip to content

Commit

Permalink
add_source_file tets finally work!
Browse files Browse the repository at this point in the history
  • Loading branch information
yobeonline committed Oct 6, 2024
1 parent 6ee25d3 commit ffa30b8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 29 deletions.
25 changes: 12 additions & 13 deletions io1_add_target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,21 @@ function(apply_source_files_properties)
endfunction()

# adds src to target, applying options
function(io1_add_source_file target src)
io1_parse_file_options("${src}" file options)

function(io1_add_source_file target file props)
get_target_property(type ${target} TYPE)
if (${type} STREQUAL "INTERFACE_LIBRARY")
target_sources(${target} INTERFACE ${file})
else()
target_sources(${target} PRIVATE ${file})
endif()

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

# any string that ends with // is a source group
Expand All @@ -288,13 +286,13 @@ function(io1_parse_file_options str out_file out_options)
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)
string(REPLACE "," ";" temp_out_options "${CMAKE_MATCH_2}")
set(${out_options}
"${temp_out_options}"
PARENT_SCOPE)
endif()
endif()
else()
Expand All @@ -307,3 +305,4 @@ function(io1_parse_file_options str out_file out_options)
)
endif()
endfunction()

99 changes: 83 additions & 16 deletions test/add_source_file.cmake
Original file line number Diff line number Diff line change
@@ -1,28 +1,95 @@
include(${CMAKE_CURRENT_LIST_DIR}/../io1_add_target.cmake)
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/foo.cpp" "int main() { return 0; }")

ct_add_test(NAME io1.add_source_file.normal)
function(${CMAKETEST_TEST})
add_library(foo STATIC)
set_target_properties(foo PROPERTIES LINKER_LANGUAGE CXX)
file(WRITE "foo/main.cpp" "int main() { return 0; }\n")

io1_add_source_file(foo "foo.cpp")
get_source_file_property(prop "foo.cpp" LANGUAGE)
ct_assert_equal(prop "NOTFOUND")
project(foo LANGUAGES CXX)
add_library(foo STATIC)
io1_add_source_file(foo "foo/main.cpp" "")

get_source_file_property(prop "foo.cpp" HEADER_FILE_ONLY)
ct_assert_equal(prop "NOTFOUND")
get_target_property(prop foo SOURCES)
ct_assert_equal(prop "foo/main.cpp") # main.cpp was added to sources

get_source_file_property(prop "foo/main.cpp" LOCATION)
ct_assert_equal(prop "${CMAKE_CURRENT_SOURCE_DIR}/foo/main.cpp")

get_source_file_property(prop "foo/main.cpp" HEADER_FILE_ONLY)
ct_assert_equal(prop "NOTFOUND") # main.cpp is not a header
endfunction()

ct_add_test(NAME io1.add_source_file.header)
function(${CMAKETEST_TEST})
add_library(foo_header STATIC)
set_target_properties(foo_header PROPERTIES LINKER_LANGUAGE CXX)

io1_add_source_file(foo_header "foo.cpp:header")
get_source_file_property(prop "foo.cpp" LANGUAGE)
#ct_assert_equal(prop "NOTFOUND")
file(WRITE "foo_header/main.cpp" "int main() { return 0; }\n")
file(WRITE "foo_header/header.cpp" "int main();\n")

get_source_file_property(prop "foo.cpp" HEADER_FILE_ONLY)
ct_assert_equal(prop "ON")
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")

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

get_source_file_property(prop "foo_header/header.cpp" LOCATION)
ct_assert_equal(prop "${CMAKE_CURRENT_SOURCE_DIR}/foo_header/header.cpp")

get_source_file_property(prop "foo_header/header.cpp" HEADER_FILE_ONLY)
ct_assert_equal(prop "ON") # header.cpp is a header
endfunction()

ct_add_test(NAME io1.add_source_file.cpp)
function(${CMAKETEST_TEST})
file(WRITE "foo_cpp/main.c" "int main() { return 0; }\n")

project(foo_cpp LANGUAGES CXX)
add_library(foo_cpp STATIC)
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

get_source_file_property(prop "foo_cpp/main.c" LANGUAGE)
ct_assert_equal(prop "CXX") # main.c is compiled as CXX and not C

get_source_file_property(prop "foo_cpp/main.c" HEADER_FILE_ONLY)
ct_assert_equal(prop "NOTFOUND") # main.c is not a header
endfunction()

ct_add_test(NAME io1.add_source_file.cpp_header)
function(${CMAKETEST_TEST})
file(WRITE "foo_cpp_header/main.cpp" "int main() { return 0; }\n")
file(WRITE "foo_cpp_header/header.c" "int main();\n")

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")

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

get_source_file_property(prop "foo_cpp_header/header.c" LANGUAGE)
ct_assert_equal(prop "CXX") # header.c is compiled as CXX and not C

get_source_file_property(prop "foo_cpp_header/header.c" HEADER_FILE_ONLY)
ct_assert_equal(prop "ON") # header.c is a header
endfunction()

ct_add_test(NAME io1.add_source_file.header_cpp)
function(${CMAKETEST_TEST})
file(WRITE "foo_header_cpp/main.cpp" "int main() { return 0; }\n")
file(WRITE "foo_header_cpp/header.c" "int main();\n")

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")

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

get_source_file_property(prop "foo_header_cpp/header.c" LANGUAGE)
ct_assert_equal(prop "CXX") # header.c is compiled as CXX and not C

get_source_file_property(prop "foo_header_cpp/header.c" HEADER_FILE_ONLY)
ct_assert_equal(prop "ON") # header.c is a header
endfunction()

0 comments on commit ffa30b8

Please sign in to comment.