Cmake_C_Project/CMakeLists.txt
2025-02-20 12:28:20 +01:00

70 lines
2.2 KiB
CMake

# Minimum required version of CMake
cmake_minimum_required(VERSION 3.20)
# Set the project name and version
project(main VERSION 1.0.0 HOMEPAGE_URL "https://git.noorlander.info/E.Noorlander/Cmake_C_Project" DESCRIPTION "Demo embedded linux C project" LANGUAGES C)
# Set a custom application name and definitions
set(BIN_NAME "test")
add_compile_definitions(APP_NAME="${BIN_NAME}")
add_compile_definitions(MESSAGE="Hello for TESTLIB from cmake.")
add_compile_definitions(MAIN_MESSAGE="Hello for main.c from cmake.")
# set 1 to compile static (Standalone)
set(BIN_STATIC 1)
# Set binary suffix based on the operating system
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(BIN_SUFFIX ".exe")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(BIN_SUFFIX ".app")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(BIN_SUFFIX ".out")
else()
set(BIN_SUFFIX ".bin")
endif()
# For Windows-specific configurations
if(MSVC)
add_compile_options(/std:c11)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
set(BUILD_SHARED_LIBS TRUE)
endif()
# List of subprojects to include (e.g., external libraries or modules)
set(SubProjects
testlib # Example subproject
)
# Create the main executable, specifying the source file
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c)
set_target_properties(${PROJECT_NAME}
PROPERTIES
OUTPUT_NAME ${BIN_NAME}
SUFFIX ${BIN_SUFFIX}
)
# Loop over the subprojects and link them to the main executable
foreach(Project IN LISTS SubProjects)
# Check if the target for this subproject has been added already
if(NOT TARGET ${Project})
# Add the subproject's directory to the build (if not already added)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib/${Project})
endif()
# Link the subproject to the main project executable
if(BIN_STATIC)
target_link_libraries(${PROJECT_NAME} PRIVATE ${Project} -static)
else()
target_link_libraries(${PROJECT_NAME} PRIVATE ${Project})
endif()
endforeach()
# Add all core's for compiling
include(ProcessorCount)
ProcessorCount(N)
if(NOT N EQUAL 0)
set(CTEST_BUILD_FLAGS -j${N})
set(ctest_test_args ${PROJECT_NAME} PARALLEL_LEVEL ${N})
endif()