Skip to content

Commit

Permalink
progress is made
Browse files Browse the repository at this point in the history
  • Loading branch information
yobeonline committed Sep 11, 2024
1 parent 1fcb9e0 commit 6ee25d3
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 206 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.19)

project(test_add_target)

include(add_target.cmake)

macro(get_cmake_test)
include(cmake_test/cmake_test OPTIONAL RESULT_VARIABLE cmake_test_found)
if(NOT cmake_test_found)
Expand All @@ -30,4 +28,5 @@ get_cmake_test()

include(cmake_test/cmake_test)

ct_add_dir("test")
enable_testing()
ct_add_dir("test" USE_REL_PATH_NAMES)
62 changes: 0 additions & 62 deletions fetch_source_files.cmake

This file was deleted.

64 changes: 0 additions & 64 deletions fetch_source_groups.cmake

This file was deleted.

86 changes: 61 additions & 25 deletions add_target.cmake → io1_add_target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function(add_target target_name)
endfunction()

set(source_group_regex "^(.*)//$")
set(source_file_properties_regex "^(.*):(.*)$")
set(source_file_properties_regex "^(.+):?(.*)$")

function(apply_source_groups)
fetch_source_groups(sources groups ${ARGN})
Expand Down Expand Up @@ -227,9 +227,11 @@ function(fetch_source_files out_sources)
PARENT_SCOPE)
endfunction()


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

Expand All @@ -245,29 +247,63 @@ function(apply_source_files_properties)
endforeach()
endfunction()

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

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()
if(io1_header)
set_source_files_properties("${file}" PROPERTIES HEADER_FILE_ONLY ON)
endif()
endfunction()

# any string that ends with // is a source group
function(io1_is_source_group str res)
if("${str}" MATCHES "^(.*)//$")
set(${res} "TRUE" PARENT_SCOPE)
else()
set(${res} "FALSE" PARENT_SCOPE)
endif()
endfunction()

# expects "file.c[:opt1,opt2,opt3,...]" and parse it into "file.c" and the list
# "opt1;opt2;opt3"
function(parse_file_options str out_file out_options)
if("${str}" MATCHES "${source_file_properties_regex}")
set(${out_file}
"${CMAKE_MATCH_1}"
PARENT_SCOPE)
if("${CMAKE_MATCH_1}" STREQUAL "")
set(${out_options}
""
PARENT_SCOPE)
else()
string(REPLACE "," ";" temp_out_options "${CMAKE_MATCH_2}")
set(${out_options}
"${temp_out_options}"
PARENT_SCOPE)
endif()
else()
set(${out_file}
"${str}"
PARENT_SCOPE)
set(${out_options}
""
PARENT_SCOPE)
endif()
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)

if("${CMAKE_MATCH_2}" STREQUAL "")
unset(${out_options}
PARENT_SCOPE)
else()
string(REPLACE "," ";" temp_out_options "${CMAKE_MATCH_2}")
set(${out_options}
"${temp_out_options}"
PARENT_SCOPE)
endif()
endif()
else()
set(${out_file}
"${str}"
PARENT_SCOPE
)
unset(${out_options}
PARENT_SCOPE
)
endif()
endfunction()
28 changes: 28 additions & 0 deletions test/add_source_file.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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)

io1_add_source_file(foo "foo.cpp")
get_source_file_property(prop "foo.cpp" LANGUAGE)
ct_assert_equal(prop "NOTFOUND")

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

get_source_file_property(prop "foo.cpp" HEADER_FILE_ONLY)
ct_assert_equal(prop "ON")
endfunction()
56 changes: 56 additions & 0 deletions test/is_source_group.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
include(${CMAKE_CURRENT_LIST_DIR}/../io1_add_target.cmake)

ct_add_test(NAME io1.is_source_group.normal)
function(${CMAKETEST_TEST})
set(out "foo")
io1_is_source_group("test.cpp" out)
ct_assert_false(out)

set(out "foo")
io1_is_source_group("test" out)
ct_assert_false(out)

set(out "foo")
io1_is_source_group("" out)
ct_assert_false(out)

set(out "foo")
io1_is_source_group("/" out)
ct_assert_false(out)

set(out "foo")
io1_is_source_group("/foo" out)
ct_assert_false(out)

set(out "foo")
io1_is_source_group("/foo/" out)
ct_assert_false(out)

set(out "foo")
io1_is_source_group("/foo/bar" out)
ct_assert_false(out)

set(out "foo")
io1_is_source_group("/foo/bar/" out)
ct_assert_false(out)

set(out "foo")
io1_is_source_group("//" out)
ct_assert_true(out)

set(out "foo")
io1_is_source_group("/foo//" out)
ct_assert_true(out)

set(out "foo")
io1_is_source_group("foo//" out)
ct_assert_true(out)

set(out "foo")
io1_is_source_group("bar/foo//" out)
ct_assert_true(out)

set(out "foo")
io1_is_source_group("/bar/foo//" out)
ct_assert_true(out)
endfunction()
Loading

0 comments on commit 6ee25d3

Please sign in to comment.