-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_cmakelists.py
125 lines (103 loc) · 3.91 KB
/
_cmakelists.py
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
def cmake_minimum_required(version):
return f"cmake_minimum_required(VERSION {version})"
def setup_cmakelists(lib_name, cpp_version, is_header_only, tests_need_imgui: bool):
from _utils import make_file
from os.path import join
make_file(
"CMakeLists.txt",
cmake_minimum_required("3.20")
+ "\n\n"
+ f'set(WARNINGS_AS_ERRORS_FOR_{lib_name.upper()} OFF CACHE BOOL "ON iff you want to treat warnings as errors")\n'
+ cmakelists_body(lib_name, cpp_version, is_header_only),
)
make_file(
join("tests", "CMakeLists.txt"),
f"""{cmake_minimum_required("3.20")}
project({lib_name}-tests)
# ---Create executable---
add_executable(${{PROJECT_NAME}} tests.cpp)
target_compile_features(${{PROJECT_NAME}} PRIVATE {cpp_version})
{setup_warnings(lib_name, "${PROJECT_NAME}")}
# ---Include our library---
add_subdirectory(.. ${{CMAKE_CURRENT_SOURCE_DIR}}/build/{lib_name})
target_link_libraries(${{PROJECT_NAME}} PRIVATE {lib_name}::{lib_name})
# ---Add doctest---
include(FetchContent)
FetchContent_Declare(
doctest
GIT_REPOSITORY https://github.com/doctest/doctest
GIT_TAG b7c21ec5ceeadb4951b00396fc1e4642dd347e5f
)
FetchContent_MakeAvailable(doctest)
target_link_libraries(${{PROJECT_NAME}} PRIVATE doctest::doctest){f'''
# ---Add quick_imgui---
include(FetchContent)
FetchContent_Declare(
quick_imgui
GIT_REPOSITORY https://github.com/CoolLibs/quick_imgui
GIT_TAG ee3de223f1d3b3ebed1e6b34963694b7e579a946
)
FetchContent_MakeAvailable(quick_imgui)
target_include_directories({lib_name} SYSTEM PRIVATE ${{quick_imgui_SOURCE_DIR}}/lib ${{quick_imgui_SOURCE_DIR}}/lib/imgui) # Give our library access to Dear ImGui
target_link_libraries(${{PROJECT_NAME}} PRIVATE quick_imgui::quick_imgui)''' if tests_need_imgui else ""}
# ---Ignore .vscode/settings.json in Git---
find_package(Git QUIET)
if(GIT_FOUND)
get_filename_component(PARENT_DIR ${{CMAKE_CURRENT_SOURCE_DIR}} DIRECTORY)
if (EXISTS "${{PARENT_DIR}}/.git")
execute_process(COMMAND ${{GIT_EXECUTABLE}} update-index --assume-unchanged .vscode/settings.json
WORKING_DIRECTORY ${{PARENT_DIR}}
RESULT_VARIABLE ERRORS)
if(NOT ERRORS EQUAL "0")
message("Git assume-unchanged failed: ${{ERRORS}}")
endif()
else()
message("No Git repository found.")
endif()
else()
message("Git executable not found.")
endif()
""",
)
def setup_warnings(lib_name, target_name):
return f"""# ---Set warning level---
if(MSVC)
target_compile_options({target_name} PRIVATE /W4)
else()
target_compile_options({target_name} PRIVATE -Wall -Wextra -Wpedantic -pedantic-errors -Wconversion -Wsign-conversion -Wimplicit-fallthrough)
endif()
# ---Maybe enable warnings as errors---
if(WARNINGS_AS_ERRORS_FOR_{lib_name.upper()})
if(MSVC)
target_compile_options({target_name} PRIVATE /WX)
else()
target_compile_options({target_name} PRIVATE -Werror)
endif()
endif()"""
def cmakelists_body(lib_name, cpp_version, is_header_only):
if is_header_only:
return f"""
add_library({lib_name} INTERFACE)
add_library({lib_name}::{lib_name} ALIAS {lib_name})
target_compile_features({lib_name} INTERFACE {cpp_version})
if(WARNINGS_AS_ERRORS_FOR_{lib_name.upper()})
target_include_directories({lib_name} INTERFACE include)
else()
target_include_directories({lib_name} SYSTEM INTERFACE include)
endif()
"""
else:
return f"""
add_library({lib_name})
add_library({lib_name}::{lib_name} ALIAS {lib_name})
target_compile_features({lib_name} PUBLIC {cpp_version})
# ---Add source files---
if(WARNINGS_AS_ERRORS_FOR_{lib_name.upper()})
target_include_directories({lib_name} PUBLIC include)
else()
target_include_directories({lib_name} SYSTEM PUBLIC include)
endif()
file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS src/*.cpp)
target_sources({lib_name} PRIVATE ${{SRC_FILES}})
{setup_warnings(lib_name, lib_name)}
"""