Skip to content

Commit

Permalink
leave SOURCES to last position
Browse files Browse the repository at this point in the history
  • Loading branch information
igormcoelho committed May 21, 2024
1 parent 5c25ae2 commit d19c8ca
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 50 deletions.
48 changes: 28 additions & 20 deletions cxxbuild/cxxbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,37 +491,21 @@ def generate_cmakelists(cppstd, root_path, INCLUDE_DIRS, DEFINITIONS, EXTRA_SOUR
for d in DEFINITIONS:
# TODO: check definition type! type1: JUST_DEF type2: DEF="value"
cmakelists.append("add_definitions(-D"+d+")")
# add sources!
cmakelists.append("set(SOURCES")
# add extra sources!
for esrc in EXTRA_SOURCES:
cmakelists.append(esrc)
# get all sources not in main() list (no duplicates exist)
clean_list = []
clean_list_main = []
for f in src_list:
clean_list.append(f.replace("\\", "/"))
#clean_list = list(set(clean_list)) # do not do this!
for filepath, app_name in src_main.items():
clean_list_main.append(filepath.replace("\\", "/"))
#clean_list_main = list(set(clean_list_main)) # do not do this!
clean_list = [x for x in clean_list if x not in clean_list_main]
for f in clean_list:
cmakelists.append("\t"+f)
cmakelists.append(")")
#
cmakelists.append("# add all executables")
COUNT_APP_ID=1
all_apps = []
# add_executable for binaries
for filepath, app_name in src_main.items():
cmakelists.append("add_executable("+app_name[1]+" "+filepath.replace("\\", "/")+" ${SOURCES})")
cmakelists.append("add_executable("+app_name[1]+" "+filepath.replace("\\", "/")+" )")
# add_executable for test binaries
print("finding test executables!")
# if no main is found, then each test is assumed to be independent!
if len(src_test_main.items()) == 0:
print("WARNING: no main() is found for tests... using main-less strategy!")
src_test_main = src_test_nomain
for filepath, app_name in src_test_main.items():
cmakelists.append("add_executable("+app_name[1]+" "+filepath.replace("\\", "/")+" ${SOURCES})")
cmakelists.append("add_executable("+app_name[1]+" "+filepath.replace("\\", "/")+" )")


# INCLUDE_DIRS will act as header-only libraries
Expand All @@ -540,6 +524,30 @@ def generate_cmakelists(cppstd, root_path, INCLUDE_DIRS, DEFINITIONS, EXTRA_SOUR
# cxxdeps.txt
cmakelists = get_cmakelists_from_cxxdeps(root_path, cmakelists, INCLUDE_DIRS, src_main, src_test_main)

# ======== begin add sources ========
cmakelists.append("# finally, add all sources")
cmakelists.append("set(SOURCES")
# add extra sources!
for esrc in EXTRA_SOURCES:
cmakelists.append("\t"+esrc)
# get all sources not in main() list (no duplicates exist)
clean_list = []
clean_list_main = []
for f in src_list:
clean_list.append(f.replace("\\", "/"))
#clean_list = list(set(clean_list)) # do not do this!
for filepath, app_name in src_main.items():
clean_list_main.append(filepath.replace("\\", "/"))
#clean_list_main = list(set(clean_list_main)) # do not do this!
clean_list = [x for x in clean_list if x not in clean_list_main]
for f in clean_list:
cmakelists.append("\t"+f)
cmakelists.append(")")
# ======== end add sources ========
for filepath, app_name in src_main.items():
cmakelists.append("target_sources("+app_name[1]+" PRIVATE ${SOURCES})")
for filepath, app_name in src_test_main.items():
cmakelists.append("target_sources("+app_name[1]+" PRIVATE ${SOURCES})")


# Generate CMakeLists.txt (or look for other option, such as 'bazel')
Expand Down
16 changes: 10 additions & 6 deletions demo/project1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
Include(FetchContent)
set(SOURCES
src/something.cpp
)
add_executable(demo1 src/demo1.cpp ${SOURCES})
add_executable(my_test tests/my_test.cpp ${SOURCES})
# add all executables
add_executable(demo1 src/demo1.cpp )
add_executable(my_test tests/my_test.cpp )
add_library(my_headers0 INTERFACE)
target_include_directories(my_headers0 INTERFACE include)
target_link_libraries(demo1 PRIVATE my_headers0)
Expand Down Expand Up @@ -40,4 +38,10 @@ IF (UNIX AND NOT APPLE) # linux
target_link_libraries(my_headers0 INTERFACE crypto)
target_link_libraries(demo1 PRIVATE crypto)
target_link_libraries(my_test PRIVATE crypto)
ENDIF()
ENDIF()
# finally, add all sources
set(SOURCES
src/something.cpp
)
target_sources(demo1 PRIVATE ${SOURCES})
target_sources(my_test PRIVATE ${SOURCES})
5 changes: 4 additions & 1 deletion demo/project2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
Include(FetchContent)
# add all executables
add_executable(demo2 src/demo2.cpp )
# finally, add all sources
set(SOURCES
src/something.cpp
)
add_executable(demo2 src/demo2.cpp ${SOURCES})
target_sources(demo2 PRIVATE ${SOURCES})
16 changes: 10 additions & 6 deletions demo/project3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
Include(FetchContent)
set(SOURCES
src/something.cpp
)
add_executable(demo3 src/demo3.cpp ${SOURCES})
add_executable(my_test tests/my_test.cpp ${SOURCES})
# add all executables
add_executable(demo3 src/demo3.cpp )
add_executable(my_test tests/my_test.cpp )
add_library(my_headers0 INTERFACE)
target_include_directories(my_headers0 INTERFACE include)
target_link_libraries(demo3 PRIVATE my_headers0)
Expand All @@ -33,4 +31,10 @@ ENDIF()
FetchContent_Declare(Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.5.1)
FetchContent_MakeAvailable(Catch2)
target_link_libraries(my_test PRIVATE Catch2::Catch2WithMain)
# cxxdeps dependency catch2
# cxxdeps dependency catch2
# finally, add all sources
set(SOURCES
src/something.cpp
)
target_sources(demo3 PRIVATE ${SOURCES})
target_sources(my_test PRIVATE ${SOURCES})
16 changes: 10 additions & 6 deletions demo/project4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
Include(FetchContent)
set(SOURCES
src/something.cpp
)
add_executable(demo4 src/demo4.cpp ${SOURCES})
add_executable(my_test tests/my_test.cpp ${SOURCES})
# add all executables
add_executable(demo4 src/demo4.cpp )
add_executable(my_test tests/my_test.cpp )
add_library(my_headers0 INTERFACE)
target_include_directories(my_headers0 INTERFACE include)
target_link_libraries(demo4 PRIVATE my_headers0)
Expand All @@ -19,4 +17,10 @@ target_link_libraries(my_test PRIVATE my_headers0)
FetchContent_Declare(Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.5.1)
FetchContent_MakeAvailable(Catch2)
target_link_libraries(my_test PRIVATE Catch2::Catch2WithMain)
# cxxdeps dependency catch2
# cxxdeps dependency catch2
# finally, add all sources
set(SOURCES
src/something.cpp
)
target_sources(demo4 PRIVATE ${SOURCES})
target_sources(my_test PRIVATE ${SOURCES})
14 changes: 9 additions & 5 deletions demo/project5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
Include(FetchContent)
set(SOURCES
)
add_executable(demo5 src/demo5.cpp ${SOURCES})
add_executable(my_test tests/my_test.cpp ${SOURCES})
# add all executables
add_executable(demo5 src/demo5.cpp )
add_executable(my_test tests/my_test.cpp )
add_library(my_headers0 INTERFACE)
target_include_directories(my_headers0 INTERFACE include)
target_link_libraries(demo5 PRIVATE my_headers0)
Expand All @@ -23,4 +22,9 @@ target_link_libraries(my_test PRIVATE fmt)
# cxxdeps dependency Catch2
FetchContent_Declare(Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.5.1)
FetchContent_MakeAvailable(Catch2)
target_link_libraries(my_test PRIVATE Catch2::Catch2WithMain)
target_link_libraries(my_test PRIVATE Catch2::Catch2WithMain)
# finally, add all sources
set(SOURCES
)
target_sources(demo5 PRIVATE ${SOURCES})
target_sources(my_test PRIVATE ${SOURCES})
15 changes: 9 additions & 6 deletions demo/project6/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
Include(FetchContent)
add_definitions(-DMY_HELLO="ola mundo")
add_definitions(-DIS_TEST)
set(SOURCES
src/demo6.cpp
src/demo6.cpp
)
add_executable(demo6 src/demo6.cpp ${SOURCES})
# add all executables
add_executable(demo6 src/demo6.cpp )
add_library(my_headers0 INTERFACE)
target_include_directories(my_headers0 INTERFACE inc)
target_link_libraries(demo6 PRIVATE my_headers0)
Expand All @@ -33,4 +30,10 @@ target_link_libraries(demo6 PRIVATE absl::log_initialize)
FetchContent_Declare(optframe GIT_REPOSITORY https://github.com/optframe/optframe.git GIT_TAG master)
FetchContent_MakeAvailable(optframe)
target_link_libraries(my_headers0 INTERFACE OptFrameAll)
target_link_libraries(demo6 PRIVATE OptFrameAll)
target_link_libraries(demo6 PRIVATE OptFrameAll)
# finally, add all sources
set(SOURCES
src/demo6.cpp
src/demo6.cpp
)
target_sources(demo6 PRIVATE ${SOURCES})

0 comments on commit d19c8ca

Please sign in to comment.