|
| 1 | +CMake Boilerplate |
| 2 | +================= |
| 3 | + |
| 4 | +..code-block::cmake |
| 5 | +
|
| 6 | + if (${TESTING}) |
| 7 | + # setup mocking |
| 8 | + # setup testing executables |
| 9 | +
|
| 10 | + # TODO, Add example |
| 11 | + endif() |
| 12 | +
|
| 13 | + if(${BUILDCC_BUILD_AS_SINGLE_LIB}) |
| 14 | + # buildcc files as an aggregate to one CMake library |
| 15 | + # third party libraries still remain seperate so do NOT add it here |
| 16 | + # Add third party library dependency to `buildcc` library in `buildcc/CMakeLists.txt` |
| 17 | +
|
| 18 | + # TODO, Add example |
| 19 | + endif() |
| 20 | +
|
| 21 | + if(${BUILDCC_BUILD_AS_INTERFACE}) |
| 22 | + # one buildcc library broken up into smaller library chunks instead of aggregated to one CMake library like in BUILDCC_BUILD_AS_SINGLE_LIB |
| 23 | + # NOTE: Do not forget to add this small library chunk to `buildcc_i` library in `buildcc/CMakeLists.txt` |
| 24 | +
|
| 25 | + # TODO, Add example |
| 26 | + endif() |
| 27 | +
|
| 28 | + if (${BUILDCC_INSTALL}) |
| 29 | + # Install behaviour when option selected |
| 30 | +
|
| 31 | + # TODO, Add example |
| 32 | + endif() |
| 33 | +
|
| 34 | +
|
| 35 | +When structuring our code we would like to create different folders with ``CMakeLists.txt`` files as individual compile units. |
| 36 | +We can then ``add_subdirectory`` that particular folder. This helps us keep our codebase modular. |
| 37 | + |
| 38 | + |
| 39 | +**Example: Environment** |
| 40 | + |
| 41 | +..code-block::cmake |
| 42 | +
|
| 43 | + # Env test |
| 44 | + if (${TESTING}) |
| 45 | + add_library(mock_env STATIC |
| 46 | + mock/logging.cpp |
| 47 | + mock/assert_fatal.cpp |
| 48 | +
|
| 49 | + src/env.cpp |
| 50 | + src/task_state.cpp |
| 51 | +
|
| 52 | + src/command.cpp |
| 53 | + mock/execute.cpp |
| 54 | + ) |
| 55 | + target_include_directories(mock_env PUBLIC |
| 56 | + ${CMAKE_CURRENT_SOURCE_DIR}/include |
| 57 | + ${CMAKE_CURRENT_SOURCE_DIR}/mock/include |
| 58 | + ) |
| 59 | + target_link_libraries(mock_env PUBLIC |
| 60 | + fmt::fmt-header-only |
| 61 | + Taskflow |
| 62 | +
|
| 63 | + CppUTest |
| 64 | + CppUTestExt |
| 65 | + gcov |
| 66 | + ) |
| 67 | + target_compile_options(mock_env PUBLIC ${TEST_COMPILE_FLAGS} ${BUILD_COMPILE_FLAGS}) |
| 68 | + target_link_options(mock_env PUBLIC ${TEST_LINK_FLAGS} ${BUILD_LINK_FLAGS}) |
| 69 | +
|
| 70 | + # Tests |
| 71 | + add_executable(test_env_util test/test_env_util.cpp) |
| 72 | + target_link_libraries(test_env_util PRIVATE mock_env) |
| 73 | +
|
| 74 | + add_executable(test_task_state test/test_task_state.cpp) |
| 75 | + target_link_libraries(test_task_state PRIVATE mock_env) |
| 76 | +
|
| 77 | + add_executable(test_command test/test_command.cpp) |
| 78 | + target_link_libraries(test_command PRIVATE mock_env) |
| 79 | +
|
| 80 | + add_test(NAME test_env_util COMMAND test_env_util) |
| 81 | + add_test(NAME test_task_state COMMAND test_task_state) |
| 82 | + add_test(NAME test_command COMMAND test_command) |
| 83 | + endif() |
| 84 | +
|
| 85 | + set(ENV_SRCS |
| 86 | + src/env.cpp |
| 87 | + src/assert_fatal.cpp |
| 88 | + src/logging.cpp |
| 89 | + include/env/assert_fatal.h |
| 90 | + include/env/assert_throw.h |
| 91 | + include/env/env.h |
| 92 | + include/env/logging.h |
| 93 | + include/env/util.h |
| 94 | +
|
| 95 | + include/env/host_os.h |
| 96 | + include/env/host_compiler.h |
| 97 | + include/env/host_os_util.h |
| 98 | +
|
| 99 | + src/task_state.cpp |
| 100 | + include/env/task_state.h |
| 101 | +
|
| 102 | + src/command.cpp |
| 103 | + src/execute.cpp |
| 104 | + include/env/command.h |
| 105 | + ) |
| 106 | +
|
| 107 | + if(${BUILDCC_BUILD_AS_SINGLE_LIB}) |
| 108 | + target_sources(buildcc PRIVATE |
| 109 | + ${ENV_SRCS} |
| 110 | + ) |
| 111 | + target_include_directories(buildcc PUBLIC |
| 112 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 113 | + $<INSTALL_INTERFACE:${BUILDCC_INSTALL_HEADER_PREFIX}> |
| 114 | + ) |
| 115 | + endif() |
| 116 | +
|
| 117 | + if(${BUILDCC_BUILD_AS_INTERFACE}) |
| 118 | + m_clangtidy("env") |
| 119 | + add_library(env |
| 120 | + ${ENV_SRCS} |
| 121 | + ) |
| 122 | + target_include_directories(env PUBLIC |
| 123 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 124 | + $<INSTALL_INTERFACE:${BUILDCC_INSTALL_HEADER_PREFIX}> |
| 125 | + ) |
| 126 | + target_link_libraries(env PUBLIC fmt::fmt-header-only) |
| 127 | + target_compile_options(env PRIVATE ${BUILD_COMPILE_FLAGS}) |
| 128 | + target_link_options(env PRIVATE ${BUILD_LINK_FLAGS}) |
| 129 | + target_link_libraries(env PRIVATE |
| 130 | + spdlog::spdlog_header_only |
| 131 | + tiny-process-library::tiny-process-library |
| 132 | + ) |
| 133 | + endif() |
| 134 | +
|
| 135 | + if (${BUILDCC_INSTALL}) |
| 136 | + if (${BUILDCC_BUILD_AS_INTERFACE}) |
| 137 | + install(TARGETS env DESTINATION lib EXPORT envConfig) |
| 138 | + install(EXPORT envConfig DESTINATION "${BUILDCC_INSTALL_LIB_PREFIX}/env") |
| 139 | + endif() |
| 140 | + install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION "${BUILDCC_INSTALL_HEADER_PREFIX}") |
| 141 | + endif() |