Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CMake on macOS and Clang #1522

Open
aaronfranke opened this issue Jul 16, 2024 · 1 comment
Open

Add support for CMake on macOS and Clang #1522

aaronfranke opened this issue Jul 16, 2024 · 1 comment
Labels
bug This has been identified as a bug cmake platform:macos topic:buildsystem Related to the buildsystem or CI setup

Comments

@aaronfranke
Copy link
Member

aaronfranke commented Jul 16, 2024

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.

@dsnopek dsnopek added bug This has been identified as a bug cmake topic:buildsystem Related to the buildsystem or CI setup platform:macos labels Aug 7, 2024
@Max-Beier
Copy link

Max-Beier commented Sep 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This has been identified as a bug cmake platform:macos topic:buildsystem Related to the buildsystem or CI setup
Projects
None yet
Development

No branches or pull requests

3 participants