SDL icon indicating copy to clipboard operation
SDL copied to clipboard

Set target property for macos to fix compilation when compiling with xcode

Open dashandslash opened this issue 3 years ago • 33 comments
trafficstars

Set target property for macOS to fix compilation when compiling with code

Description

On macOS with SDL 2.24.0, after generating an Xcode project with CMake (version 3.24.1) that compiles and links to SDL-static, multiple errors are thrown during compilation. The errors thrown are similar to "Unkown type name NSString" image

Adding the compile flag "-x objective-c" seems to fix the problem.

set_target_properties(SDL2-static PROPERTIES COMPILE_FLAGS "-x objective-c")

dashandslash avatar Sep 07 '22 10:09 dashandslash

Older cmake versions have issues with the latest sdl2 release because it includes .m objective-c sources, for which CMake has only added support in recent versions.

Also, -x objective-c means "compile this source as objective-c", which I think is not correct.

What CMake version are you using? Older CMake versions have issues cross building from e.g. x86_64 to armv8.

madebr avatar Sep 07 '22 11:09 madebr

I am using cmake version 3.24.1, which should be quite updated

dashandslash avatar Sep 07 '22 11:09 dashandslash

One note, if I generate a Ninja project, compilation works just fine with SDL 2.24.0

While with Xcode I have to add the above flag

dashandslash avatar Sep 07 '22 11:09 dashandslash

I am using cmake version 3.24.1, which should be quite updated

Great, that rules things out.

Can you try adding enable_language(OBJC) somewhere in an appropriate location?

madebr avatar Sep 07 '22 11:09 madebr

Sure I can try to add enable_language(OBJC), in the sdl cmakelists?

Because compilation fails on SDL-static, not on my project

dashandslash avatar Sep 07 '22 11:09 dashandslash

Sure I can try to add enable_language(OBJC), in the sdl cmakelists?

Because compilation fails on SDL-static, not on my project

Yes, it should be in sdl's cmakelists because it is us that is using .m sources.

madebr avatar Sep 07 '22 11:09 madebr

The downside of my proposal to use enable_language(OBJC) is that is increases the required CMake version to 3.16 (for Apple systems) An alternative could be to add the following:

foreach(src ${SOURCE_FILES})
    if(src MATCHES ".*\.m$")
        set_property(SOURCE "${src}" APPEND_STRING COMPILE_FLAGS " -x objective-c")
    endif()
endforeach()

somewhere before the first add_library(sdl2 ...) is called.

madebr avatar Sep 07 '22 11:09 madebr

So I tried to quickly replace set_target_properties(SDL2-static PROPERTIES COMPILE_FLAGS "-x objective-c") with your suggested macro enable_language(OBJC)

But no luck :(

Also I noticed that for cmake version 3.16+ enable_language(OBJC) is called here: https://github.com/libsdl-org/SDL/blob/d4e0d27c1c21e364d21be3af681f02b05f362345/cmake/macros.cmake#L105

dashandslash avatar Sep 07 '22 11:09 dashandslash

foreach(src ${SOURCE_FILES})
    if(src MATCHES ".*\.m$")
        set_property(SOURCE "${src}" APPEND_STRING COMPILE_FLAGS " -x objective-c")
    endif()
endforeach()

It's a brute force approach, but definitely better than my proposal 😅

dashandslash avatar Sep 07 '22 11:09 dashandslash

Can you please post the error log for a clean build of sdl2/sdl2-static? Because perhaps the error is because some objective-c header is included in a C source.

madebr avatar Sep 07 '22 11:09 madebr

foreach(src ${SOURCE_FILES})
    if(src MATCHES ".*\.m$")
        set_property(SOURCE "${src}" APPEND_STRING COMPILE_FLAGS " -x objective-c")
    endif()
endforeach()

Of course, it seems the issue is thrown from SDL_sysfilesystem.m


Showing Recent Messages
CompileC /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/blackboard_projects.build/Debug/SDL2-static.build/Objects-normal/arm64/SDL_sysfilesystem.o /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/filesystem/cocoa/SDL_sysfilesystem.m normal arm64 c com.apple.compilers.llvm.clang.1_0.compiler (in target 'SDL2-static' from project 'blackboard_projects')
    cd /Users/luca/dev/dashandslash/blackboard_app_template
    export LANG\=en_US.US-ASCII
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c -target arm64-apple-macos12.3 -fmessage-length\=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit\=0 -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-float-conversion -Wno-non-literal-null-conversion -Wno-objc-literal-conversion -Wno-shorten-64-to-32 -Wpointer-sign -Wno-newline-eof -DCMAKE_INTDIR\=\"Debug\" -DSDL_STATIC_LIB -DUSING_GENERATED_CONFIG_H -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fstrict-aliasing -Wdeprecated-declarations -g -Wno-sign-conversion -Wno-infinite-recursion -Wno-comma -Wno-block-capture-autoreleasing -Wno-strict-prototypes -Wno-semicolon-before-method-body -index-store-path /Users/luca/Library/Developer/Xcode/DerivedData/blackboard_projects-epfzwdcjsptvawhbxsxatsajmyvz/Index/DataStore -I/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/Debug/include -I/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/include -I/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/include-config-debug -I/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/blackboard_projects.build/Debug/SDL2-static.build/DerivedSources-normal/arm64 -I/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/blackboard_projects.build/Debug/SDL2-static.build/DerivedSources/arm64 -I/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/blackboard_projects.build/Debug/SDL2-static.build/DerivedSources -F/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/Debug -idirafter /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/video/khronos -Wall -fno-strict-aliasing -Werror\=declaration-after-statement -Wdeclaration-after-statement -fvisibility\=hidden -Wshadow -D_THREAD_SAFE -DSDL_BUILD_MAJOR_VERSION\=2 -DSDL_BUILD_MINOR_VERSION\=25 -DSDL_BUILD_MICRO_VERSION\=0 -fobjc-arc -MMD -MT dependencies -MF /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/blackboard_projects.build/Debug/SDL2-static.build/Objects-normal/arm64/SDL_sysfilesystem.d --serialize-diagnostics /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/blackboard_projects.build/Debug/SDL2-static.build/Objects-normal/arm64/SDL_sysfilesystem.dia -c /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/filesystem/cocoa/SDL_sysfilesystem.m -o /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-build/blackboard_projects.build/Debug/SDL2-static.build/Objects-normal/arm64/SDL_sysfilesystem.o -index-unit-output-path /blackboard_projects.build/Debug/SDL2-static.build/Objects-normal/arm64/SDL_sysfilesystem.o

In file included from /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/filesystem/cocoa/SDL_sysfilesystem.m:28:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:523:1: error: expected identifier or '('
@class NSString, Protocol;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:525:9: error: unknown type name 'NSString'
typedef NSString * NSExceptionName NS_TYPED_EXTENSIBLE_ENUM;
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:526:9: error: unknown type name 'NSString'
typedef NSString * NSRunLoopMode NS_TYPED_EXTENSIBLE_ENUM;
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:528:19: error: unknown type name 'NSString'
FOUNDATION_EXPORT NSString *NSStringFromSelector(SEL aSelector);
                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:529:44: error: unknown type name 'NSString'
FOUNDATION_EXPORT SEL NSSelectorFromString(NSString *aSelectorName);
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:531:19: error: unknown type name 'NSString'
FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass);
                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:532:53: error: unknown type name 'NSString'
FOUNDATION_EXPORT Class _Nullable NSClassFromString(NSString *aClassName);
                                                    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:534:19: error: unknown type name 'NSString'
FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:534:50: error: unknown type name 'Protocol'
FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                                                 ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:535:19: error: unknown type name 'Protocol'
FOUNDATION_EXPORT Protocol * _Nullable NSProtocolFromString(NSString *namestr) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:535:61: error: unknown type name 'NSString'
FOUNDATION_EXPORT Protocol * _Nullable NSProtocolFromString(NSString *namestr) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                                                            ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:539:30: error: unknown type name 'NSString'
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NS_NO_TAIL_CALL;
                             ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:539:53: error: format argument not an NSString
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NS_NO_TAIL_CALL;
                             ~~~~~~~~~~~~~~~~       ^                  ~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:94:49: note: expanded from macro 'NS_FORMAT_FUNCTION'
        #define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))
                                                       ^                    ~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:540:31: error: unknown type name 'NSString'
FOUNDATION_EXPORT void NSLogv(NSString *format, va_list args) NS_FORMAT_FUNCTION(1,0) NS_NO_TAIL_CALL;
                              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:540:63: error: format argument not an NSString
FOUNDATION_EXPORT void NSLogv(NSString *format, va_list args) NS_FORMAT_FUNCTION(1,0) NS_NO_TAIL_CALL;
                              ~~~~~~~~~~~~~~~~                ^                  ~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:94:49: note: expanded from macro 'NS_FORMAT_FUNCTION'
        #define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))
                                                       ^                    ~
In file included from /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/filesystem/cocoa/SDL_sysfilesystem.m:28:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:9:1: error: expected identifier or '('
@class NSString;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:19:63: error: unknown type name 'NSString'
FOUNDATION_EXPORT void NSSetZoneName(NSZone * _Nullable zone, NSString *name)NS_SWIFT_UNAVAILABLE("Zone-based memory management is unavailable");
                                                              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:20:19: error: unknown type name 'NSString'
FOUNDATION_EXPORT NSString *NSZoneName(NSZone * _Nullable zone) NS_SWIFT_UNAVAILABLE("Zone-based memory management is unavailable");
                  ^
In file included from /Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/filesystem/cocoa/SDL_sysfilesystem.m:28:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:10:1: error: expected identifier or '('
@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

dashandslash avatar Sep 07 '22 11:09 dashandslash

@madebr this suggestion

    foreach(src ${SOURCE_FILES})
        if(src MATCHES ".*/.m$")
            set_property(SOURCE "${src}" APPEND_STRING COMPILE_FLAGS " -x objective-c")
        endif()
    endforeach()

Doesn't look like it's working, unfortunately. So far the only way for me to compile SDL-static is to add set_target_properties(SDL2-static PROPERTIES COMPILE_FLAGS "-x objective-c")

🤷🏻

dashandslash avatar Sep 07 '22 12:09 dashandslash

Thanks for the log. Curiously, it passes -x c to the compiler which means it interprets the source as c. This really should not happen.

Have you confirmed that the enable(OBJC) is actually executed? This can be done by running cmake in trace mode:

cmake --trace .. 2>&1 | grep OBJC

Also, can you provide the value of some CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS variables?

message("CMAKE_OBJC_SOURCE_FILE_EXTENSIONS: ${CMAKE_OBJC_SOURCE_FILE_EXTENSIONS}")
message("CMAKE_C_SOURCE_FILE_EXTENSIONS: ${CMAKE_C_SOURCE_FILE_EXTENSIONS}")
message("CMAKE_CXX_SOURCE_FILE_EXTENSIONS: ${CMAKE_CXX_SOURCE_FILE_EXTENSIONS}")

madebr avatar Sep 07 '22 12:09 madebr

Have you confirmed that the enable(OBJC) is actually executed?

Yes, enable_language(OBJC) is called

Also, can you provide the value of some CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS variables?

Sure

.build/CMakeFiles/3.24.1/CMakeOBJCCompiler.cmake

set(CMAKE_OBJC_COMPILER "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang")
set(CMAKE_OBJC_COMPILER_ARG1 "")
set(CMAKE_OBJC_COMPILER_ID "AppleClang")
set(CMAKE_OBJC_COMPILER_VERSION "13.1.6.13160021")
set(CMAKE_OBJC_COMPILER_VERSION_INTERNAL "")
set(CMAKE_OBJC_COMPILER_WRAPPER "")
set(CMAKE_OBJC_STANDARD_COMPUTED_DEFAULT "11")
set(CMAKE_OBJC_EXTENSIONS_COMPUTED_DEFAULT "ON")
set(CMAKE_OBJC_COMPILE_FEATURES "")
set(CMAKE_OBJC90_COMPILE_FEATURES "")
set(CMAKE_OBJC99_COMPILE_FEATURES "")
set(CMAKE_OBJC11_COMPILE_FEATURES "")
set(CMAKE_OBJC17_COMPILE_FEATURES "")
set(CMAKE_OBJC23_COMPILE_FEATURES "")

set(CMAKE_OBJC_PLATFORM_ID "Darwin")
set(CMAKE_OBJC_SIMULATE_ID "")
set(CMAKE_OBJC_COMPILER_FRONTEND_VARIANT "")
set(CMAKE_OBJC_SIMULATE_VERSION "")

set(CMAKE_XCODE_ARCHS "arm64")
set(CMAKE_AR "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar")
set(CMAKE_OBJC_COMPILER_AR "")
set(CMAKE_RANLIB "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib")
set(CMAKE_OBJC_COMPILER_RANLIB "")
set(CMAKE_LINKER "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld")
set(CMAKE_MT "")
set(CMAKE_COMPILER_IS_GNUOBJC )
set(CMAKE_OBJC_COMPILER_LOADED 1)
set(CMAKE_OBJC_COMPILER_WORKS TRUE)
set(CMAKE_OBJC_ABI_COMPILED TRUE)

set(CMAKE_OBJC_COMPILER_ENV_VAR "OBJC")

set(CMAKE_OBJC_COMPILER_ID_RUN 1)
set(CMAKE_OBJC_SOURCE_FILE_EXTENSIONS m)
set(CMAKE_OBJC_IGNORE_EXTENSIONS h;H;o;O)
set(CMAKE_OBJC_LINKER_PREFERENCE 5)

foreach (lang C CXX OBJCXX)
  foreach(extension IN LISTS CMAKE_OBJC_SOURCE_FILE_EXTENSIONS)
    if (CMAKE_${lang}_COMPILER_ID_RUN)
      list(REMOVE_ITEM CMAKE_${lang}_SOURCE_FILE_EXTENSIONS ${extension})
    endif()
  endforeach()
endforeach()

# Save compiler ABI information.
set(CMAKE_OBJC_SIZEOF_DATA_PTR "8")
set(CMAKE_OBJC_COMPILER_ABI "")
set(CMAKE_OBJC_BYTE_ORDER "LITTLE_ENDIAN")
set(CMAKE_OBJC_LIBRARY_ARCHITECTURE "")

if(CMAKE_OBJC_SIZEOF_DATA_PTR)
  set(CMAKE_SIZEOF_VOID_P "${CMAKE_OBJC_SIZEOF_DATA_PTR}")
endif()

if(CMAKE_OBJC_COMPILER_ABI)
  set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_OBJC_COMPILER_ABI}")
endif()

if(CMAKE_OBJC_LIBRARY_ARCHITECTURE)
  set(CMAKE_LIBRARY_ARCHITECTURE "")
endif()





set(CMAKE_OBJC_IMPLICIT_INCLUDE_DIRECTORIES "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.1.6/include;/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include;/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include")
set(CMAKE_OBJC_IMPLICIT_LINK_LIBRARIES "objc")
set(CMAKE_OBJC_IMPLICIT_LINK_DIRECTORIES "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib")
set(CMAKE_OBJC_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks")

.build/CMakeFiles/3.24.1/CMakeCCompiler.cmake

set(CMAKE_C_COMPILER "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang")
set(CMAKE_C_COMPILER_ARG1 "")
set(CMAKE_C_COMPILER_ID "AppleClang")
set(CMAKE_C_COMPILER_VERSION "13.1.6.13160021")
set(CMAKE_C_COMPILER_VERSION_INTERNAL "")
set(CMAKE_C_COMPILER_WRAPPER "")
set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "17")
set(CMAKE_C_EXTENSIONS_COMPUTED_DEFAULT "ON")
set(CMAKE_C_COMPILE_FEATURES "c_std_90;c_function_prototypes;c_std_99;c_restrict;c_variadic_macros;c_std_11;c_static_assert;c_std_17;c_std_23")
set(CMAKE_C90_COMPILE_FEATURES "c_std_90;c_function_prototypes")
set(CMAKE_C99_COMPILE_FEATURES "c_std_99;c_restrict;c_variadic_macros")
set(CMAKE_C11_COMPILE_FEATURES "c_std_11;c_static_assert")
set(CMAKE_C17_COMPILE_FEATURES "c_std_17")
set(CMAKE_C23_COMPILE_FEATURES "c_std_23")

set(CMAKE_C_PLATFORM_ID "Darwin")
set(CMAKE_C_SIMULATE_ID "")
set(CMAKE_C_COMPILER_FRONTEND_VARIANT "")
set(CMAKE_C_SIMULATE_VERSION "")



set(CMAKE_XCODE_ARCHS "arm64")
set(CMAKE_AR "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar")
set(CMAKE_C_COMPILER_AR "")
set(CMAKE_RANLIB "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib")
set(CMAKE_C_COMPILER_RANLIB "")
set(CMAKE_LINKER "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld")
set(CMAKE_MT "")
set(CMAKE_COMPILER_IS_GNUCC )
set(CMAKE_C_COMPILER_LOADED 1)
set(CMAKE_C_COMPILER_WORKS TRUE)
set(CMAKE_C_ABI_COMPILED TRUE)

set(CMAKE_C_COMPILER_ENV_VAR "CC")

set(CMAKE_C_COMPILER_ID_RUN 1)
set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m)
set(CMAKE_C_IGNORE_EXTENSIONS h;H;o;O;obj;OBJ;def;DEF;rc;RC)
set(CMAKE_C_LINKER_PREFERENCE 10)

# Save compiler ABI information.
set(CMAKE_C_SIZEOF_DATA_PTR "8")
set(CMAKE_C_COMPILER_ABI "")
set(CMAKE_C_BYTE_ORDER "LITTLE_ENDIAN")
set(CMAKE_C_LIBRARY_ARCHITECTURE "")

if(CMAKE_C_SIZEOF_DATA_PTR)
  set(CMAKE_SIZEOF_VOID_P "${CMAKE_C_SIZEOF_DATA_PTR}")
endif()

if(CMAKE_C_COMPILER_ABI)
  set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_C_COMPILER_ABI}")
endif()

if(CMAKE_C_LIBRARY_ARCHITECTURE)
  set(CMAKE_LIBRARY_ARCHITECTURE "")
endif()

set(CMAKE_C_CL_SHOWINCLUDES_PREFIX "")
if(CMAKE_C_CL_SHOWINCLUDES_PREFIX)
  set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_C_CL_SHOWINCLUDES_PREFIX}")
endif()





set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.1.6/include;/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include;/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include")
set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib")
set(CMAKE_C_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks")

dashandslash avatar Sep 07 '22 12:09 dashandslash

@madebr I can tell you that replacing enable_language(OBJC) with enable_language(OBJCXX) does the trick. Maybe we might need to actually keep both

I will clean all my builds and retest this fixup

dashandslash avatar Sep 07 '22 12:09 dashandslash

Your logs contain

set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m)

Can you try adding the following after enable_language(OBJC) (without adding OBJCXX)

list(REMOVE CMAKE_C_SOURCE_FILE_EXTENSIONS m)

madebr avatar Sep 07 '22 12:09 madebr

Your logs contain

set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m)

Can you try adding the following after enable_language(OBJC) (without adding OBJCXX)

list(REMOVE CMAKE_C_SOURCE_FILE_EXTENSIONS m)

I tried that, but for some reason it does not remove the ;m from set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m)

Even removing that by hand, compilation fails with the same errors

dashandslash avatar Sep 07 '22 12:09 dashandslash

Are you building sdl standalone or as a subproject? Can you try doing:

project(SDL2 C CXX OBJC)

at the top of our cmake scrip? (instead of we doing only project(SDL2 C CXX OBJC))

madebr avatar Sep 07 '22 13:09 madebr

Are you building sdl standalone or as a subproject? Can you try doing:

project(SDL2 C CXX OBJC) at the top of our cmake scrip? (instead of we doing only project(SDL2 C CXX OBJC))

I am building SDL with my project by including the sources using CMake FetchContent_Declare like so:

# SDL
FetchContent_Declare(sdl
    GIT_REPOSITORY [email protected]:dashandslash/SDL.git
    # GIT_TAG fix-compilation-on-osx-with-cmake-xcode-project-generation
    GIT_TAG main
    GIT_SHALLOW 1
)
FetchContent_GetProperties(sdl)
if(NOT sdl_POPULATED)
    FetchContent_Populate(sdl)

    if(APPLE)
        set(SDL_VIDEO_METAL 1)
    endif()

	  add_subdirectory(${sdl_SOURCE_DIR} ${sdl_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

I also tried:

FetchContent_Declare(sdl
    GIT_REPOSITORY [email protected]:dashandslash/SDL.git
    # GIT_TAG fix-compilation-on-osx-with-cmake-xcode-project-generation
    GIT_TAG main
    GIT_SHALLOW 1
)
FetchContent_MakeAvailable(sdl) 

I did try editing the SDL CMakeLists.txt as you suggested by replacing it with project(SDL2 C CXX OBJC), and I got the same errors when compiling.

So far I got it compile only when adding

  if(APPLE)
    set_target_properties(SDL2-static PROPERTIES COMPILE_FLAGS "-x objective-c")
  endif()

I will try to reconfigure the project with and without the above change and diff the resulting cmake in the build folder. Let's see if I can narrow down what is happening with such a change 🤷🏻‍♂️

dashandslash avatar Sep 07 '22 17:09 dashandslash

FetchContent is basically doing add_subdirectory. To iterate faster, you should be able to fetch from a local repo and replace the argument of GIT_REPOSITORY with a path on your filesystem. Or replace it with add_subdirectory(/path/to/checked/out/modified/sdl2 SDL).

Your suggestion of adding set_target_properties(SDL2-static PROPERTIES COMPILE_FLAGS "-x objective-c") might work, but it looks so wrong because you basically force every file to be compiled as objective-c.

Can you try changing the project(YOURPROJECT) to also include OBJC? Also, can you make sure to always run cmake from a "fresh" directory? The caching from cmake might influence things.

I created an issue at CMake's repo. Let's hope the developers know what happened to you :smile:

madebr avatar Sep 07 '22 18:09 madebr

@madebr thanks so much for your help and for taking the time to post in the CMake repo!

I will try all the above, and always from a clean folder. I cannot guarantee I did that with the previous tests, it's easy to make a mistake when you do almost the same thing multiple times 😅

dashandslash avatar Sep 07 '22 19:09 dashandslash

@madebr I manage to set the property only for the .m files as you previously suggested. I think the regular expression that I checked against previously did not work so well.

I just changed that into this:

  if(APPLE)
    foreach(source_file ${SOURCE_FILES})
      get_filename_component(file_extension ${source_file} EXT)
      if(file_extension MATCHES "m")
        set_source_files_properties(${source_file} PROPERTIES COMPILE_FLAGS "-x objective-c++")
      endif()
    endforeach()
  endif()

And compiles just fine now 🎉

I think this could be a valid solution.

dashandslash avatar Sep 07 '22 19:09 dashandslash

That could be a valid solution indeed, but with some modifications/questions:

  • Does adding -x objective-c instead of -x objective-c++ work as well?
  • Doing set_source_files_properties(${source_file} PROPERTIES COMPILE_FLAGS "-x objective-c++") overwrites the currently set compile flags, better is to append to the string using set_property(SOURCE "${source_file}" APPEND_STRING COMPILE_FLAGS " -x objective-c") (or objective-c++ if objective-c does not work) `

madebr avatar Sep 07 '22 20:09 madebr

That could be a valid solution indeed, but with some modifications/questions:

  • Does adding -x objective-c instead of -x objective-c++ work as well?
  • Doing set_source_files_properties(${source_file} PROPERTIES COMPILE_FLAGS "-x objective-c++") overwrites the currently set compile flags, better is to append to the string using set_property(SOURCE "${source_file}" APPEND_STRING COMPILE_FLAGS " -x objective-c") (or objective-c++ if objective-c does not work) `

Nice catch!

setting -x objective-c and appending the compile flags works perfectly

  if(APPLE)
    foreach(SOURCE_FILE ${SOURCE_FILES})
      get_filename_component(FILE_EXTENSION ${SOURCE_FILE} EXT)
      if(${FILE_EXTENSION} MATCHES "m")
        set_source_files_properties(${SOURCE_FILE} APPEND_STRING COMPILE_FLAGS " -x objective-c")
      endif()
    endforeach()
  endif()

dashandslash avatar Sep 07 '22 20:09 dashandslash

set_source_file_properties does not (officially) support appending. So you need to use set_property(SOURCE ... APPEND_STRING ...).

madebr avatar Sep 07 '22 20:09 madebr

set_source_file_properties does not (officially) support appending. So you need to use set_property(SOURCE ... APPEND_STRING ...).

Sure, I pushed the change.

dashandslash avatar Sep 07 '22 20:09 dashandslash

Does this relate at all to https://github.com/libsdl-org/SDL/commit/6da6a6cba2a7c857890063073f47445584617bcb?

slouken avatar Sep 07 '22 21:09 slouken

Does this relate at all to 6da6a6c?

Yes it does, CMAKE_<LANG>_FLAGS are special variables in that they apply on all targets in the current directory, even those that are already created. (very counter-intuitive)

@dashandslash Can you play a bit with that? What is the value of CMAKE_C_FLAGS before that assignment? What is the value of CMAKE_OBJC_FLAGS before that assignment?

madebr avatar Sep 07 '22 21:09 madebr

Does this relate at all to 6da6a6c?

Yes it does, CMAKE_<LANG>_FLAGS are special variables in that they apply on all targets in the current directory, even those that are already created. (very counter-intuitive)

@dashandslash Can you play a bit with that? What is the value of CMAKE_C_FLAGS before that assignment? What is the value of CMAKE_OBJC_FLAGS before that assignment?

Sure, I can have a look at it

dashandslash avatar Sep 07 '22 21:09 dashandslash

@madebr

This is the output for CMAKE_C_FLAGS CMAKE_OBJC_FLAGS before and after set(CMAKE_OBJC_FLAGS "${CMAKE_OBJC_FLAGS} ${CMAKE_C_FLAGS}")

CMAKE_C_FLAGS    Before  -idirafter "/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/video/khronos"  -Wall -fno-strict-aliasing -Werror=declaration-after-statement -Wdeclaration-after-statement -fvisibility=hidden -Wshadow -D_THREAD_SAFE  -DSDL_BUILD_MAJOR_VERSION=2 -DSDL_BUILD_MINOR_VERSION=25 -DSDL_BUILD_MICRO_VERSION=0
CMAKE_OBJC_FLAGS Before 

set(CMAKE_OBJC_FLAGS "${CMAKE_OBJC_FLAGS} ${CMAKE_C_FLAGS}")

CMAKE_C_FLAGS    After  -idirafter "/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/video/khronos"  -Wall -fno-strict-aliasing -Werror=declaration-after-statement -Wdeclaration-after-statement -fvisibility=hidden -Wshadow -D_THREAD_SAFE  -DSDL_BUILD_MAJOR_VERSION=2 -DSDL_BUILD_MINOR_VERSION=25 -DSDL_BUILD_MICRO_VERSION=0
CMAKE_OBJC_FLAGS After  -idirafter "/Users/luca/dev/dashandslash/blackboard_app_template/.external/blackboard_app-src/.external/sdl-src/src/video/khronos"  -Wall -fno-strict-aliasing -Werror=declaration-after-statement -Wdeclaration-after-statement -fvisibility=hidden -Wshadow -D_THREAD_SAFE  -DSDL_BUILD_MAJOR_VERSION=2 -DSDL_BUILD_MINOR_VERSION=25 -DSDL_BUILD_MICRO_VERSION=0

dashandslash avatar Sep 08 '22 09:09 dashandslash