godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.73k stars 570 forks source link

Add support for CMake on macOS and Clang #1522

Open aaronfranke opened 3 months ago

aaronfranke commented 3 months ago

Godot version

Master of godot-cpp

godot-cpp version

Master of godot-cpp

System information

macOS 14.5

Issue description

I'm not able to build using CMake on macOS:

cmake .
cmake --build .

Gives this error:

clang: error: unsupported option '-static-libgcc'

I believe this is caused by the current CMakeLists.txt file assuming that anything that isn't MSVC is GCC:

target_link_options(${PROJECT_NAME} PRIVATE
    $<$<NOT:${compiler_is_msvc}>:
        -static-libgcc
        -static-libstdc++
        -Wl,-R,'$$ORIGIN'
    >
)

To fix this, in the CMakeLists.txt file, we should:

Steps to reproduce

Try to build with CMake on macOS.

Minimal reproduction project

Master branch of godot-cpp.

Max-Beier commented 1 month ago

I've managed to compile it to a DyLib with apple's native Clang installed on 14.6.1. Not sure if that's the issue.

cmake_minimum_required(VERSION 3.13)

project(Convenience VERSION 0.0.1 LANGUAGES CXX)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)

# Download Godot Bindings using FetchContent
include(FetchContent)

FetchContent_Declare(
    GDExtension
    GIT_REPOSITORY https://github.com/godotengine/godot-cpp.git
    GIT_TAG godot-4.3-stable
)

FetchContent_MakeAvailable(GDExtension)

# Collect all source files (.h, .hpp, .cpp) in the src directory
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.[ch]pp" "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")

add_library(${PROJECT_NAME} SHARED ${SOURCES})

# Set output directory for DLLs/shared libraries
set(LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/project/bin")

# Determine architecture (x64 or x86)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(ARCHITECTURE "x64")
else()
    set(ARCHITECTURE "x86")
endif()

# Lowercase Build Type
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)

# Platform-specific settings
if (WIN32)
    # Windows settings with architecture-specific output names
    set_target_properties(${PROJECT_NAME} PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        RUNTIME_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        OUTPUT_NAME "convenience.windows.template_${ARCHITECTURE}_${CMAKE_BUILD_TYPE_LOWER}"
    )
elseif (APPLE)
    # macOS settings without architecture-specific output names
    set_target_properties(${PROJECT_NAME} PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        RUNTIME_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        OUTPUT_NAME "convenience.macos.template_${CMAKE_BUILD_TYPE_LOWER}"
    )
elseif (UNIX)
    # Linux settings without architecture-specific output names
    set_target_properties(${PROJECT_NAME} PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        RUNTIME_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_DIRECTORY}"
        OUTPUT_NAME "convenience.linux.template_${CMAKE_BUILD_TYPE_LOWER}"
    )
endif()

# Include the src directory for headers
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")

# Link the library with Godot's C++ bindings
target_link_libraries(${PROJECT_NAME} PUBLIC godot::cpp)

# Organize source files in the project tree
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX src FILES ${SOURCES})