|
1 | 1 | # Using the same minimum as the godot-cpp project |
2 | 2 | cmake_minimum_required(VERSION 3.17) |
3 | 3 |
|
4 | | -# Specify Options |
5 | | -option(USE_GIT_SUBMODULES ON "") |
6 | | -option(USE_FETCHCONTENT OFF "") |
7 | | - |
8 | | -# Verify Options |
9 | | -if(USE_GIT_SUBMODULES AND USE_FETCHCONTENT) |
10 | | - message(FATAL_ERROR "Cannot specify both git submodules and fetchcontent.") |
11 | | -endif() |
12 | | - |
13 | | -#[[ CMake has a bunch of global properties which get copied to projects and targets at the moment of definition. |
14 | | -So make any changes to global options which effect all components of the build prior to making any call that |
15 | | -leads to a project or target definition. |
16 | | -Examples are: CMAKE_OSX_ARCHITECTURES, CMAKE_MSVC_RUNTIME_LIBRARY |
17 | | -]] |
18 | | - |
19 | 4 | # Silence unused variable warning when specified from toolchain |
20 | 5 | if(CMAKE_C_COMPILER) |
21 | 6 | endif() |
22 | 7 |
|
| 8 | +set(LIBNAME "EXTENSION-NAME" CACHE STRING "The name of the library") |
| 9 | +set(GODOT_PROJECT_DIR "demo" CACHE STRING "The directory of a Godot project folder") |
23 | 10 |
|
24 | 11 | # Make sure all the dependencies are satisfied |
25 | 12 | find_package(Python3 3.4 REQUIRED) |
26 | | - |
27 | | -# For godot-cpp we can use the git submodule method, or we can use CMake's fetchcontent module |
28 | | -# In either case it is important to specify any GODOTCPP_ options prior to add_subdirectory |
29 | | -# or fetchcontent_makeavailable |
30 | | - |
31 | | -#set(GODOTCPP_BUILD_PROFILE "${CMAKE_CURRENT_SOURCE_DIR}/build_profile.json") |
32 | | - |
33 | | -if(USE_GIT_SUBMODULES) |
34 | | - if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/godot-cpp/src") |
35 | | - message(NOTICE "godot-cpp bindings source not found") |
36 | | - message(NOTICE "initializing/updating the godot-cpp submodule...") |
37 | | - |
38 | | - # update the c++ bindings submodule to populate it with |
39 | | - # the necessary source for the library |
40 | | - execute_process( |
41 | | - COMMAND git submodule update --init extern/godot-cpp |
42 | | - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
43 | | - COMMAND_ERROR_IS_FATAL ANY |
44 | | - ) |
45 | | - endif() |
46 | | - add_subdirectory(godot-cpp) |
47 | | -endif() |
48 | | - |
49 | | -if(USE_FETCHCONTENT) |
50 | | - include(FetchContent) |
51 | | - # Godot-cpp |
52 | | - set(GODOTCPP_GIT_URL "http://github.com/godotengine/godot-cpp.git" CACHE STRING "The git url of godot-cpp to fetch") |
53 | | - set(GODOTCPP_GIT_BRANCH "master" CACHE STRING "The git branch of godot-cpp to fetch") |
54 | | - |
55 | | - fetchcontent_declare(godot-cpp |
56 | | - GIT_REPOSITORY ${GODOTCPP_GIT_URL} |
57 | | - GIT_TAG ${GODOTCPP_GIT_BRANCH} |
58 | | - GIT_PROGRESS ON |
59 | | - SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/godot-cpp" |
| 13 | +find_program(GIT git REQUIRED) |
| 14 | + |
| 15 | +# Ensure godot-cpp submodule has been updated |
| 16 | +if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/godot-cpp/src") |
| 17 | + message(NOTICE "godot-cpp bindings source not found") |
| 18 | + message(NOTICE "initializing/updating the godot-cpp submodule...") |
| 19 | + |
| 20 | + # update the c++ bindings submodule to populate it with the necessary source for the library |
| 21 | + execute_process( |
| 22 | + COMMAND git submodule update --init godot-cpp |
| 23 | + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} |
| 24 | + COMMAND_ERROR_IS_FATAL ANY |
60 | 25 | ) |
61 | | - fetchcontent_makeavailable(godot-cpp) |
62 | 26 | endif() |
| 27 | +add_subdirectory(godot-cpp SYSTEM) |
| 28 | + |
| 29 | +# Add godot-cpp's module path and include the exported functions. |
| 30 | +# This is made available for documentation generation |
| 31 | +set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${godot-cpp_SOURCE_DIR}/cmake") |
| 32 | +include(GodotCPPModule) |
| 33 | + |
| 34 | +# The godot-cpp target has some of useful properties attached that can be retrieved like so. |
| 35 | +get_target_property(GODOTCPP_SUFFIX godot::cpp GODOTCPP_SUFFIX) |
| 36 | +get_target_property(GODOTCPP_PLATFORM godot::cpp GODOTCPP_PLATFORM) |
63 | 37 |
|
64 | | -# Now we can specify our own project. |
| 38 | +# Now we can specify our own project which will inherit any global cmake properties or variables that have been defined. |
65 | 39 | project(godot-cpp-template |
66 | 40 | VERSION 1.0 |
67 | 41 | DESCRIPTION "This repository serves as a quickstart template for GDExtension development with Godot 4.0+." |
68 | 42 | HOMEPAGE_URL "https://github.com/enetheru/godot-cpp-template/tree/main" |
69 | 43 | LANGUAGES CXX |
70 | 44 | ) |
71 | 45 |
|
72 | | -# The PROJECT_NAME stores the name of the last called project() |
73 | | -add_library(${PROJECT_NAME} SHARED) |
| 46 | +add_library(${LIBNAME} SHARED) |
74 | 47 |
|
75 | | -target_sources(${PROJECT_NAME} |
| 48 | +target_sources(${LIBNAME} |
76 | 49 | PRIVATE |
77 | 50 | src/register_types.cpp |
78 | 51 | src/register_types.h |
79 | 52 | ) |
80 | 53 |
|
81 | | -target_link_libraries(${PROJECT_NAME} PRIVATE godot-cpp.editor) |
| 54 | +# Fetch a list of the xml files to use for documentation and add to our target |
| 55 | +file(GLOB_RECURSE DOC_XML LIST_DIRECTORIES NO CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/doc_classes/*.xml") |
| 56 | + |
| 57 | +# conditionally add doc data to compile output |
| 58 | +if(DOC_XML) |
| 59 | + if(GODOTCPP_TARGET MATCHES "editor|template_debug") |
| 60 | + target_doc_sources(${LIBNAME} ${DOC_XML}) |
| 61 | + endif() |
| 62 | +endif() |
82 | 63 |
|
83 | | -# The godot targets have a couple of useful properties attached to them |
84 | | -get_target_property(GODOTCPP_SUFFIX godot-cpp.editor GODOTCPP_SUFFIX) |
| 64 | +target_link_libraries(${LIBNAME} PRIVATE godot-cpp) |
85 | 65 |
|
86 | | -set_target_properties(${PROJECT_NAME} |
| 66 | +set_target_properties(${LIBNAME} |
87 | 67 | PROPERTIES |
88 | | - #The generator expression here prevents a subdir from being created. |
89 | | - RUNTIME_OUTPUT_DIRECTORY "$<1:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}>" |
90 | | - # name format project.<platform>.<target>[.dev][.double].<arch>[.custom_suffix] |
91 | | - OUTPUT_NAME "${PROJECT_NAME}${GODOTCPP_SUFFIX}" |
| 68 | + # The generator expression here prevents msvc from adding a Debug or Release subdir. |
| 69 | + RUNTIME_OUTPUT_DIRECTORY "$<1:${PROJECT_SOURCE_DIR}/bin/${GODOTCPP_PLATFORM}>" |
| 70 | + |
| 71 | + PREFIX "" |
| 72 | + OUTPUT_NAME "${LIBNAME}${GODOTCPP_SUFFIX}" |
| 73 | +) |
| 74 | + |
| 75 | +set(GODOT_PROJECT_BINARY_DIR "${PROJECT_SOURCE_DIR}/${GODOT_PROJECT_DIR}/bin/${GODOTCPP_PLATFORM}") |
| 76 | + |
| 77 | +add_custom_command(TARGET ${LIBNAME} POST_BUILD |
| 78 | + COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:${LIBNAME}>" "${GODOT_PROJECT_BINARY_DIR}/$<TARGET_FILE_NAME:${LIBNAME}>" |
92 | 79 | ) |
0 commit comments