-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cmake
executable file
·64 lines (56 loc) · 2.2 KB
/
build.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env cmake -P
# CMake arguments up to '-P' (ignore these)
set(i "1")
set(COMMAND_ARG "")
set(CONFIG_ARG "")
while(i LESS "${CMAKE_ARGC}")
if("${CMAKE_ARGV${i}}" STREQUAL "clean")
set(COMMAND_ARG "${CMAKE_ARGV${i}}")
elseif("${CMAKE_ARGV${i}}" STREQUAL "Release")
set(CONFIG_ARG "${CMAKE_ARGV${i}}")
elseif("${CMAKE_ARGV${i}}" STREQUAL "Debug")
set(CONFIG_ARG "${CMAKE_ARGV${i}}")
endif()
math(EXPR i "${i} + 1") # next argument
endwhile()
if(CONFIG_ARG STREQUAL "")
set(CONFIG_ARG "Release")
endif()
# Define the build directory
set(BUILD_DIR "${CMAKE_BINARY_DIR}/build")
# If the command is "clean", clean the build directory
if(COMMAND_ARG STREQUAL "clean")
message(STATUS "Cleaning build directory...")
file(REMOVE_RECURSE ${BUILD_DIR})
return() # Exit the script after cleaning
endif()
# Check if the system supports multi-config
set(MULTI_CONFIG_FOUND "FALSE")
if(EXISTS ${BUILD_DIR}/CMakeCache.txt)
file(READ "${BUILD_DIR}/CMakeCache.txt" CMAKE_CACHE_CONTENTS)
string(FIND "${CMAKE_CACHE_CONTENTS}" "CMAKE_CONFIGURATION_TYPES" MULTI_CONFIG_FOUND_INDEX)
if(MULTI_CONFIG_FOUND_INDEX GREATER -1)
set(MULTI_CONFIG_FOUND "TRUE")
message(STATUS "Multi-config system found.")
endif()
endif()
set(BUILD_SYSTEM, "")
if(NOT MULTI_CONFIG_FOUND)
message(STATUS "Setting build system to Ninja")
set(BUILD_SYSTEM, "-G Ninja")
endif()
# Configure the project for single-config systems or if CMakeCache is missing
if(NOT MULTI_CONFIG_FOUND OR NOT EXISTS ${BUILD_DIR}/CMakeCache.txt)
message(STATUS "Configuring project for configuration: ${CONFIG_ARG}")
execute_process(
COMMAND ${CMAKE_COMMAND} ${BUILD_SYSTEM} -S ${CMAKE_SOURCE_DIR} -B ${BUILD_DIR} -DCMAKE_BUILD_TYPE=${CONFIG_ARG}
)
endif()
# Build the project with the appropriate configuration
if(MULTI_CONFIG_FOUND)
message(STATUS "Detected multi-config system. Building with configuration: ${CONFIG_ARG}")
execute_process(COMMAND ${CMAKE_COMMAND} --build ${BUILD_DIR} --config ${CONFIG_ARG})
else()
message(STATUS "Detected single-config system. Building with configuration: ${CONFIG_ARG}")
execute_process(COMMAND ${CMAKE_COMMAND} --build ${BUILD_DIR})
endif()