axmolengine / axmol

Axmol Engine – A Multi-platform Engine for Desktop, XBOX (UWP) and Mobile games. (A fork of Cocos2d-x-4.0)
https://axmol.dev
MIT License
922 stars 205 forks source link

CMake fails on macOS #1724

Closed smilediver closed 8 months ago

smilediver commented 8 months ago

I have a weird issue where running CMake for building my project, or Axmol, or Axmol's generated project, targeting macOS fails with this error:

CMake Error in core/CMakeLists.txt:
  The language OBJCXX was requested for compilation but was not enabled.  To
  enable a language it needs to be specified in a 'project' or
  'enable_language' command in the root CMakeLists.txt

I have to add enable_language(OBJCXX) to fix the issue like this:

project(...)
if (APPLE)
    enable_language(OBJCXX)
endif()

I can't figure out if this is a real issue and enable_language(OBJCXX) should be added to Axmol's and project templates CMakeLists.txt files, or there's something wrong with my setup. Does anyone building for macOS had the same issue?

P.S. I'm using brew's CMake version 3.28.1

rh101 commented 8 months ago

The languages should be specified in the project command.

If an empty new project is created for macOS and cmake fails with that error, then yes, this should be added to the template CMakeLists.txt. If an empty new project does not fail, then this shouldn't be added to the template, since it's not an issue with the template.

smilediver commented 8 months ago

Yes, building a newly generated empty project by Axmol fails. Building Axmol itself also fails.

I think I've found what triggers the issue. If I comment out lines like these: set_source_files_properties(media/MediaEngine.cpp PROPERTIES LANGUAGE OBJCXX), then the issue goes away. This probably triggers the requirement of enabling OBJCXX language, since by default CMake enables only C and C++ languages.

I've checked how a project using Cocos works, that doesn't specify languages in the project's CMake files. In this case Objective-C/C++ files are passed to C/C++ compiler without indicating a different language. Compiler probably detects the language by file extension in this case. So in this case CMake enables only C and C++ compiler and doesn't know that it actually compiles Objective-C/C++ files, but since compiler detects the language automatically it happens to work.

Btw, the language can't be easily specified using project(), because then Android build fails trying to check for Objective-C/C++ compiler. The full fix should be like this:

project(...)
if (APPLE)
    enable_language(OBJC OBJCXX)
endif()

or

set(LANGUAGES C CXX)
if(APPLE)
    list(APPEND LANGUAGES OBJC OBJCXX)
endif()
project(${APP_NAME} LANGUAGES ${LANGUAGES})

The first one looks a bit cleaner. I'll make a PR later.

rh101 commented 8 months ago

Btw, the language can't be easily specified using project(), because then Android build fails trying to check for Objective-C/C++ compiler. The full fix should be like this:

I'm not sure what you mean, because I know for a fact that this works:

if(APPLE)
    project(${APP_NAME} LANGUAGES C CXX OBJC OBJCXX)
else()
    project(${APP_NAME} LANGUAGES C CXX)
endif()

I wasn't actually implying that all languages be added to project, but rather only the ones required for the specific platform.

smilediver commented 8 months ago

I wasn't actually implying that all languages be added to project, but rather only the ones required for the specific platform.

Ah, ok, then I misunderstood you. :) So now we have 3 ways to fix this, which one should I do?

rh101 commented 8 months ago

Ah, ok, then I misunderstood you. :) So now we have 3 ways to fix this, which one should I do?

It's so strange though, because I just did a test with the following:

axmol new -p org.axmol.test2 -d . -l cpp test2 axmol build -p osx -a x64 -c

No cmake errors at all.

Then the project is opened in Xcode 15.0.1 (Mac M1 Mini with MacOS Ventura 13.6.1), and built successfully. Nothing was changed in the CMakeLists.txt, so no languages explicitly set in the project command. I'm curious to know what is different about your setup to cause this issue.

Regarding which method to use, either of the following would be the preference, since they're both straightforward:

project(...)
if (APPLE)
    enable_language(OBJC OBJCXX)
endif()

or

if(APPLE)
    project(${APP_NAME} LANGUAGES C CXX OBJC OBJCXX)
else()
    project(${APP_NAME} LANGUAGES C CXX)
endif()
halx99 commented 8 months ago

I guess you use cmake generate command directly, and not specify -GXcode

the axmol build command will auto set -GXcode for APPLE platforms(macos, tvos, ios)

smilediver commented 8 months ago

Yes, you're partially right, I'm using VSCode/ninja setup for development. Just tried it and cmake -G Ninja -B build . fails, but cmake -G Xcode -B build . works.