Skip to content

Conversation

ericwa
Copy link
Contributor

@ericwa ericwa commented Nov 27, 2021

Description

Without this change, I was getting errors about ObjC syntax from headers being used in the test program which was being compiled as C.

Confirmed to work on:
macOS 10.15.7
XCode Version 12.4 (12D4e)
Xcode generator (cmake -GXcode ..)

Existing Issue(s)

Fixes #5010
(I think) fixes #3893

Confirmed to work on:
macOS 10.15.7
XCode Version 12.4 (12D4e)
@slouken
Copy link
Collaborator

slouken commented Nov 27, 2021

So this has gone back and forth over the past few years, e.g. 42c5b4a

Can you take a look at the history here and see what makes sense?

@slouken slouken added this to the 2.0.20 milestone Nov 27, 2021
@ericwa
Copy link
Contributor Author

ericwa commented Nov 27, 2021

Ahh ok, I will check the history on this.

Regarding this comment from the 42c5b4a commit message:

An alternative fix of course would be to use CHECK_OBJC_SOURCE_COMPILES instead of cmake's check_objc_source_compiles - but that had the same problem of getting confused by "return 0;". (Maybe that's because it's a macro? I'll defer to a cmake expert on this one.)

I think I ran into the same problem - when I first tried changing the macro to check_objc_source_compiles I was getting this bizarre cmake error:

  Unknown argument:

    HAVE_FRAMEWORK_METAL

For some reason, collapsing the main() and the end of the macro invocation onto one line fixes it:

int main(void) {}" HAVE_FRAMEWORK_METAL)

So that's why I reformatted the main() in this PR - no idea what the CMake explanation for that is!

@cgutman
Copy link
Collaborator

cgutman commented Nov 27, 2021

Our CI seems to have no trouble detecting Metal, either with or without your fix. There must be something more complex going on.

https://github.com/libsdl-org/SDL/runs/4338781399?check_suite_focus=true

@ericwa
Copy link
Contributor Author

ericwa commented Nov 27, 2021

I can reproduce that on my mac -it's the -GXcode (so generating an Xcode project, instead of makefiles) that causes metal to not be detected without this PR.

i.e.

git checkout main
cmake -B build2           # detected metal.
cmake -B build3 -GXcode   # didn't detect metal.

# switch to this branch
git checkout macos-cmake-detect-metal
cmake -B build4           # detected metal.
cmake -B build5 -GXcode   # detected metal.

@ericwa
Copy link
Contributor Author

ericwa commented Nov 27, 2021

Looking over the history, the patch from #3893 is similar to what I did here, except it used CMake 3.16's check_objc_source_compiles macro, so it was rejected for raising the required CMake version.

The use of -ObjC over -x objective-c happened in these issues/commits: #3749 #3547 e294639 .

I think we're safe from that issue though, because I'm using SDL's internal check_objc_source_compiles macro from https://github.com/libsdl-org/SDL/blob/main/cmake/macros.cmake#L96 (also used by the CoreHaptics and GameController framework checks here: https://github.com/libsdl-org/SDL/blob/main/CMakeLists.txt#L1841 ).

SDL's check_objc_source_compiles is doing:

set(CMAKE_REQUIRED_DEFINITIONS "-x objective-c ${PREV_REQUIRED_DEFS}")

but the problematic Metal check fixed by e294639 was doing:

set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -x objective-c")

(Apparently, that had the issue where -x objective-c was passed to the linker, which tried to read .o files as ObjC source code.)

@slouken
Copy link
Collaborator

slouken commented Nov 27, 2021

Okay, let's pull this in for 2.0.20. Thanks for researching this!

@icculus icculus self-assigned this Dec 4, 2021
@vkedwardli
Copy link
Contributor

Sorry to ask, will this be fixed in 2.0.20 or 2.0.22? Seems it is tagged as 2.0.22 mistakenly?

@slouken
Copy link
Collaborator

slouken commented Jan 5, 2022

This is pushed out to 2.0.22, so we have more time to catch issues with this change. 2.0.20 is going to be released shortly.

@icculus icculus merged commit 71e06a5 into libsdl-org:main Jan 26, 2022
@icculus
Copy link
Collaborator

icculus commented Jan 26, 2022

Okay, this is in; we'll reopen if any concerns pop up during the 2.0.22 dev cycle!

@sezero
Copy link
Contributor

sezero commented Jan 26, 2022

Okay, this is in; we'll reopen if any concerns pop up during the 2.0.22 dev cycle!

Maybe we also need the following? Otherwise, it is using our own macro instead
of CMake's own (c.f. @Wohlstand's patch from #3912)

diff --git a/cmake/macros.cmake b/cmake/macros.cmake
index 84cb4c6..b825676 100644
--- a/cmake/macros.cmake
+++ b/cmake/macros.cmake
@@ -92,12 +92,16 @@ macro(LISTTOSTRREV _LIST _OUTPUT)
   endforeach()
 endmacro()
 
+if(${CMAKE_VERSION} VERSION_LESS "3.16.0")
 macro(CHECK_OBJC_SOURCE_COMPILES SOURCE VAR)
   set(PREV_REQUIRED_DEFS "${CMAKE_REQUIRED_DEFINITIONS}")
   set(CMAKE_REQUIRED_DEFINITIONS "-x objective-c ${PREV_REQUIRED_DEFS}")
   CHECK_C_SOURCE_COMPILES(${SOURCE} ${VAR})
   set(CMAKE_REQUIRED_DEFINITIONS "${PREV_REQUIRED_DEFS}")
 endmacro()
+else()
+ include(CheckOBJCSourceCompiles)
+endif()
 
 if(CMAKE_VERSION VERSION_LESS 3.13.0)
   macro(target_link_directories _TARGET _SCOPE)

@sezero
Copy link
Contributor

sezero commented Feb 5, 2022

Maybe we also need the following? Otherwise, it is using our own macro instead of CMake's own (c.f. @Wohlstand's patch from #3912)

diff --git a/cmake/macros.cmake b/cmake/macros.cmake
index 84cb4c6..b825676 100644
--- a/cmake/macros.cmake
+++ b/cmake/macros.cmake
@@ -92,12 +92,16 @@ macro(LISTTOSTRREV _LIST _OUTPUT)
   endforeach()
 endmacro()
 
+if(${CMAKE_VERSION} VERSION_LESS "3.16.0")
 macro(CHECK_OBJC_SOURCE_COMPILES SOURCE VAR)
   set(PREV_REQUIRED_DEFS "${CMAKE_REQUIRED_DEFINITIONS}")
   set(CMAKE_REQUIRED_DEFINITIONS "-x objective-c ${PREV_REQUIRED_DEFS}")
   CHECK_C_SOURCE_COMPILES(${SOURCE} ${VAR})
   set(CMAKE_REQUIRED_DEFINITIONS "${PREV_REQUIRED_DEFS}")
 endmacro()
+else()
+ include(CheckOBJCSourceCompiles)
+endif()
 
 if(CMAKE_VERSION VERSION_LESS 3.13.0)
   macro(target_link_directories _TARGET _SCOPE)

PING? Is the patch above OK?

@icculus
Copy link
Collaborator

icculus commented Feb 5, 2022

Sorry, missed this before. Go ahead and commit that patch.

sezero added a commit that referenced this pull request Feb 5, 2022
@sezero
Copy link
Contributor

sezero commented Feb 5, 2022

Patch applied as 686a0f3

@sezero
Copy link
Contributor

sezero commented Feb 5, 2022

Patch applied as 686a0f3

Hmph, failures in CI with:

CMake Error at /usr/local/Cellar/cmake/3.22.2/share/cmake/Modules/Internal/CheckSourceCompiles.cmake:44 (message):
  check_source_compiles: OBJC: needs to be enabled before use.

Ideas about how to fix from cmake gurus? (Or should I just back it out?)

@sezero
Copy link
Contributor

sezero commented Feb 5, 2022

Patch applied as 686a0f3

Hmph, failures in CI with:

Figured out and fixed in git.

@pionere
Copy link
Contributor

pionere commented Mar 20, 2022

My project does not build for IOS since this change (MacOS build works). I get the following error:

CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_OBJC_COMPILE_OBJECT

The version just before works fine.
Any suggestions how to fix this?

glebm added a commit to glebm/SDL that referenced this pull request Mar 26, 2022
As of libsdl-org#5011, iOS build started
failing with the following error:

    CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
    Missing variable is:
    CMAKE_OBJC_COMPILE_OBJECT

This works around the error by simply skipping the check for iOS, since
all iOS devices released after 2013 support Metal.

Fixes libsdl-org#5452
@glebm glebm mentioned this pull request Mar 26, 2022
glebm added a commit to glebm/SDL that referenced this pull request Mar 26, 2022
As of libsdl-org#5011, iOS build started
failing with the following error:

    CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
    Missing variable is:
    CMAKE_OBJC_COMPILE_OBJECT

This works around the error by simply skipping `check_objc_source_compiles` for iOS.

Fixes libsdl-org#5452
glebm added a commit to glebm/SDL that referenced this pull request Mar 26, 2022
As of libsdl-org#5011, iOS build started
failing with the following error:

    CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
    Missing variable is:
    CMAKE_OBJC_COMPILE_OBJECT

This works around the error by simply skipping `check_objc_source_compiles` for iOS.

Fixes libsdl-org#5452
glebm added a commit to glebm/SDL that referenced this pull request Mar 26, 2022
As of libsdl-org#5011, iOS build started
failing with the following error:

    CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
    Missing variable is:
    CMAKE_OBJC_COMPILE_OBJECT

This works around the error by simply skipping `check_objc_source_compiles` for iOS.

Fixes libsdl-org#5452
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

macOS + cmake with Xcode generator: Metal not detected cmake build doesn't detect Metal on macOS
7 participants