godot-cpp icon indicating copy to clipboard operation
godot-cpp copied to clipboard

Add support for CMake on macOS and Clang

Open aaronfranke opened this issue 1 year ago • 4 comments

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:

  • Add support for Clang.
  • Add support for macOS.
  • Add a CI job to build and test CMake on macOS.

Steps to reproduce

Try to build with CMake on macOS.

Minimal reproduction project

Master branch of godot-cpp.

aaronfranke avatar Jul 16 '24 07:07 aaronfranke

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})

Max-Beier avatar Sep 04 '24 07:09 Max-Beier

@aaronfranke Can we mark this as resolved now that the modernise PR is merged?

enetheru avatar Nov 23 '24 04:11 enetheru

Tested with commit a42b3634d20b4e7088be721792bba1199277362c, it compiles fine now. And the CMake code now has explicit support for both macOS and its different architectures, so that's great. The only thing mentioned in this issue that isn't done is adding a CI check for CMake on macOS, so that we can ensure everything keeps working.

aaronfranke avatar Nov 26 '24 21:11 aaronfranke

I have a CI PR which compiles for macos, but I am missing iOS support. #1726

enetheru avatar Mar 06 '25 08:03 enetheru

@aaronfranke the CI job for macos has been working for a while now, can this be closed now?

enetheru avatar Oct 16 '25 06:10 enetheru

Yes, closing as implemented. Thank you!

aaronfranke avatar Oct 16 '25 07:10 aaronfranke