-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
137 lines (111 loc) · 4.07 KB
/
CMakeLists.txt
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
cmake_minimum_required(VERSION 3.18)
project(remote_fmt VERSION 0.1.0)
set(remote_fmt_dir
${CMAKE_CURRENT_LIST_DIR}
CACHE INTERNAL "")
add_library(remote_fmt INTERFACE)
if(NOT CMAKE_CROSSCOMPILING)
include(cmake_helpers/FindOrFetch.cmake)
find_or_fetch_package(fmt 9 GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG master)
find_or_fetch_package(nlohmann_json 3.9 GIT_REPOSITORY
https://github.com/nlohmann/json GIT_TAG master)
find_or_fetch_package(magic_enum 0.9.6 GIT_REPOSITORY
https://github.com/Neargye/magic_enum GIT_TAG v0.9.6)
else()
include(FetchContent)
FetchContent_Populate(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG master
)
add_subdirectory(${fmt_SOURCE_DIR} ${fmt_BINARY_DIR} EXCLUDE_FROM_ALL)
target_compile_definitions(remote_fmt
INTERFACE FMT_STATIC_THOUSANDS_SEPARATOR=1)
target_compile_options(
remote_fmt INTERFACE "-DFMT_ASSERT(condition, message)=assert(condition)")
FetchContent_Declare(
magic_enum
GIT_REPOSITORY https://github.com/Neargye/magic_enum
GIT_TAG master
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(magic_enum)
endif()
add_subdirectory(string_constant)
target_include_directories(remote_fmt INTERFACE src)
target_link_libraries(
remote_fmt INTERFACE string_constant::string_constant fmt::fmt-header-only
magic_enum::magic_enum)
add_library(remote_fmt::remote_fmt ALIAS remote_fmt)
if(NOT CMAKE_CROSSCOMPILING)
add_library(remote_fmt_parser INTERFACE)
target_include_directories(remote_fmt_parser INTERFACE src)
target_link_libraries(
remote_fmt_parser INTERFACE fmt::fmt magic_enum::magic_enum
nlohmann_json::nlohmann_json)
add_library(remote_fmt::parser ALIAS remote_fmt_parser)
endif()
function(target_generate_string_constants targetname)
cmake_parse_arguments(PARSE_ARGV 1 PARSED_ARGS "" "STRING_CONSTANTS_MAP_FILE"
"")
if(PARSED_ARGS_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "unknown argument ${PARSED_ARGS_UNPARSED_ARGUMENTS}")
endif()
if(NOT PARSED_ARGS_STRING_CONSTANTS_MAP_FILE)
set(PARSED_ARGS_STRING_CONSTANTS_MAP_FILE
"${targetname}_string_constants.json")
endif()
get_target_property(bin_dir ${targetname} BINARY_DIR)
target_link_options(${targetname} PUBLIC
${bin_dir}/${targetname}_string_constants.obj)
get_target_property(options_flags ${targetname} COMPILE_OPTIONS)
if("${options_flags}" STREQUAL "options_flags_NOTFOUND")
string(JOIN " " flags ${CMAKE_CXX_FLAGS})
else()
string(JOIN " " flags ${CMAKE_CXX_FLAGS} ${options_flags})
endif()
find_package(Python3 COMPONENTS Interpreter REQUIRED)
set(command
${Python3_EXECUTABLE}
${remote_fmt_dir}/tools/generate_string_constants.py
--target_name
${targetname}
--out_dir
${bin_dir}
--source_dir
${remote_fmt_dir}
--compiler
${CMAKE_CXX_COMPILER}
--objects
$<TARGET_OBJECTS:${targetname}>)
if(NOT "${flags}" STREQUAL "")
list(APPEND command --flags=${flags})
endif()
add_custom_command(
TARGET ${targetname}
PRE_LINK COMMAND_EXPAND_LISTS
COMMAND
${command} DEPENDS ${remote_fmt_dir}/tools/generate_string_constants.py
BYPRODUCS ${bin_dir}/${targetname}_string_constants.cpp
${bin_dir}/${targetname}_string_constants.obj
${bin_dir}/${targetname}_string_constants.json)
set_property(
TARGET ${targetname}
APPEND
PROPERTY LINK_DEPENDS ${remote_fmt_dir}/tools/generate_string_constants.py)
set_property(
TARGET ${targetname}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES
${bin_dir}/${targetname}_string_constants.cpp)
set_property(
TARGET ${targetname}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES
${bin_dir}/${targetname}_string_constants.obj)
set_property(
TARGET ${targetname}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES
${bin_dir}/${targetname}_string_constants.json)
endfunction()