diff options
author | Steve Wilson <stevew@wolfram.com> | 2019-09-13 17:11:15 (GMT) |
---|---|---|
committer | Cristian Adam <cristian.adam@gmail.com> | 2019-09-28 13:56:46 (GMT) |
commit | 80f120a85fb0992e3f06ad69ebdad881bea9af8c (patch) | |
tree | 9cfc710d17bd751932faae6c5dd523764bba78f3 /Tests | |
parent | 8c568722d79f9de0c03550981cbcb9453733a8bb (diff) | |
download | CMake-80f120a85fb0992e3f06ad69ebdad881bea9af8c.zip CMake-80f120a85fb0992e3f06ad69ebdad881bea9af8c.tar.gz CMake-80f120a85fb0992e3f06ad69ebdad881bea9af8c.tar.bz2 |
Languages: Add support for Objective-C
Add entries in Modules and Modules/Platform to support
Objective-C compiler determination and identification.
Add Modules to check Objective-C compiler flags, source
compilations, program checks, etc...
Use OBJC as the designator of the language, eg:
project(foo OBJC)
Add various tests for Objective-C language features. Add
tests to preserve C++ handling of .m and .mm files when
OBJC is not a configured language.
Co-Authored-By: Cristian Adam <cristian.adam@gmail.com>
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/CMakeLists.txt | 37 | ||||
-rw-r--r-- | Tests/CMakeOnly/CMakeLists.txt | 6 | ||||
-rw-r--r-- | Tests/CMakeOnly/CheckLanguage/CMakeLists.txt | 11 | ||||
-rw-r--r-- | Tests/CMakeOnly/CheckOBJCCompilerFlag/CMakeLists.txt | 17 | ||||
-rw-r--r-- | Tests/CMakeOnly/CompilerIdOBJC/CMakeLists.txt | 14 | ||||
-rw-r--r-- | Tests/Objective-C/c-file-extension-test/CMakeLists.txt | 5 | ||||
-rw-r--r-- | Tests/Objective-C/c-file-extension-test/main.m | 8 | ||||
-rw-r--r-- | Tests/Objective-C/objc-file-extension-test/CMakeLists.txt | 6 | ||||
-rw-r--r-- | Tests/Objective-C/objc-file-extension-test/main.m | 12 | ||||
-rw-r--r-- | Tests/Objective-C/simple-build-test/CMakeLists.txt | 11 | ||||
-rw-r--r-- | Tests/Objective-C/simple-build-test/foo.h | 9 | ||||
-rw-r--r-- | Tests/Objective-C/simple-build-test/foo.m | 7 | ||||
-rw-r--r-- | Tests/Objective-C/simple-build-test/main.m | 12 | ||||
-rw-r--r-- | Tests/TryCompile/CMakeLists.txt | 30 |
14 files changed, 183 insertions, 2 deletions
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index c284603..2e17e59 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -2328,6 +2328,43 @@ ${CMake_SOURCE_DIR}/Utilities/Release/push.bash --dir dev -- '${CMake_BUILD_NIGH list(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/CFBundleTest") ADD_TEST_MACRO(ObjC++ ObjC++) + + add_test(Objective-C.simple-build-test ${CMAKE_CTEST_COMMAND} + --build-and-test + "${CMake_SOURCE_DIR}/Tests/Objective-C/simple-build-test" + "${CMake_BINARY_DIR}/Tests/Objective-C/simple-build-test" + --build-two-config + ${build_generator_args} + --build-project simple-build-test + --build-options ${build_options} + --test-command simple-build-test + ) + list(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/Objective-C/simple-build-test") + + add_test(Objective-C.c-file-extension-test ${CMAKE_CTEST_COMMAND} + --build-and-test + "${CMake_SOURCE_DIR}/Tests/Objective-C/c-file-extension-test" + "${CMake_BINARY_DIR}/Tests/Objective-C/c-file-extension-test" + --build-two-config + ${build_generator_args} + --build-project c-file-extension-test + --build-options ${build_options} + --test-command c-file-extension-test + ) + list(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/Objective-C/c-file-extension-test") + + add_test(Objective-C.objc-file-extension-test ${CMAKE_CTEST_COMMAND} + --build-and-test + "${CMake_SOURCE_DIR}/Tests/Objective-C/objc-file-extension-test" + "${CMake_BINARY_DIR}/Tests/Objective-C/objc-file-extension-test" + --build-two-config + ${build_generator_args} + --build-project objc-file-extension-test + --build-options ${build_options} + --test-command objc-file-extension-test + ) + list(APPEND TEST_BUILD_DIRS "${CMAKE_BINARY_DIR}/Tests/Objective-C/objc-file-extension-test") + endif () endif () diff --git a/Tests/CMakeOnly/CMakeLists.txt b/Tests/CMakeOnly/CMakeLists.txt index 19f3f79..1a25469 100644 --- a/Tests/CMakeOnly/CMakeLists.txt +++ b/Tests/CMakeOnly/CMakeLists.txt @@ -30,6 +30,12 @@ add_CMakeOnly_test(CheckStructHasMember) add_CMakeOnly_test(CompilerIdC) add_CMakeOnly_test(CompilerIdCXX) + +if(CMAKE_OBJC_COMPILER) + add_CMakeOnly_test(CompilerIdOBJC) + add_CMakeOnly_test(CheckOBJCCompilerFlag) +endif() + if(CMAKE_Fortran_COMPILER) add_CMakeOnly_test(CompilerIdFortran) endif() diff --git a/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt b/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt index ca4becb..e9bdf6a 100644 --- a/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt +++ b/Tests/CMakeOnly/CheckLanguage/CMakeLists.txt @@ -5,10 +5,19 @@ include(CheckLanguage) set(langs ) set(expect_C 1) set(expect_CXX 1) +if(APPLE) + set(expect_OBJC 1) +endif() unset(expect_Fortran) set(expect_NoSuchLanguage 0) -foreach(lang C CXX Fortran CUDA NoSuchLanguage) +set(LANGUAGES C CXX Fortran CUDA NoSuchLanguage) + +if(APPLE) + list(APPEND LANGUAGES OBJC) +endif() + +foreach(lang ${LANGUAGES}) check_language(${lang}) if(NOT DEFINED CMAKE_${lang}_COMPILER) message(FATAL_ERROR "check_language(${lang}) did not set result") diff --git a/Tests/CMakeOnly/CheckOBJCCompilerFlag/CMakeLists.txt b/Tests/CMakeOnly/CheckOBJCCompilerFlag/CMakeLists.txt new file mode 100644 index 0000000..a9a96ee --- /dev/null +++ b/Tests/CMakeOnly/CheckOBJCCompilerFlag/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 2.8.12) + +project(CheckOBJCCompilerFlag) + +include(CheckOBJCCompilerFlag) + +if(CMAKE_COMPILER_IS_GNUOBJC) + set(COMPILER_FLAG -fobjc-direct-dispatch) +else() + set(COMPILER_FLAG -fobjc-gc) +endif() + +CHECK_OBJC_COMPILER_FLAGS(${COMPILER_FLAG} HAS_COMPILER_FLAG) + +if(NOT HAS_COMPILER_FLAG) + message(SEND_ERROR "Test fail: HAS_COMPILER_FLAG: ${COMPILER_FLAG}") +endif diff --git a/Tests/CMakeOnly/CompilerIdOBJC/CMakeLists.txt b/Tests/CMakeOnly/CompilerIdOBJC/CMakeLists.txt new file mode 100644 index 0000000..8f13787 --- /dev/null +++ b/Tests/CMakeOnly/CompilerIdOBJC/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.8.12) +project(CompilerIdOBJC OBJC) + +foreach(v + CMAKE_OBJC_COMPILER + CMAKE_OBJC_COMPILER_ID + CMAKE_OBJC_COMPILER_VERSION + ) + if(${v}) + message(STATUS "${v}=[${${v}}]") + else() + message(SEND_ERROR "${v} not set!") + endif() +endforeach() diff --git a/Tests/Objective-C/c-file-extension-test/CMakeLists.txt b/Tests/Objective-C/c-file-extension-test/CMakeLists.txt new file mode 100644 index 0000000..e091448 --- /dev/null +++ b/Tests/Objective-C/c-file-extension-test/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.15) + +project(c-file-extension-test C) + +add_executable(c-file-extension-test main.m) diff --git a/Tests/Objective-C/c-file-extension-test/main.m b/Tests/Objective-C/c-file-extension-test/main.m new file mode 100644 index 0000000..1c159a9 --- /dev/null +++ b/Tests/Objective-C/c-file-extension-test/main.m @@ -0,0 +1,8 @@ +#ifndef __OBJC__ +# error "Compiler cannot compile Objective-C" +#endif + +int main() +{ + return 0; +} diff --git a/Tests/Objective-C/objc-file-extension-test/CMakeLists.txt b/Tests/Objective-C/objc-file-extension-test/CMakeLists.txt new file mode 100644 index 0000000..27e88be --- /dev/null +++ b/Tests/Objective-C/objc-file-extension-test/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.15) + +project(objc-file-extension-test OBJC CXX) + +add_executable(objc-file-extension-test main.m) +target_link_libraries(objc-file-extension-test "-framework Foundation") diff --git a/Tests/Objective-C/objc-file-extension-test/main.m b/Tests/Objective-C/objc-file-extension-test/main.m new file mode 100644 index 0000000..2ec3917 --- /dev/null +++ b/Tests/Objective-C/objc-file-extension-test/main.m @@ -0,0 +1,12 @@ +#ifndef __OBJC__ +# error "Compiler is not an Objective-C compiler." +#endif + +#import <Foundation/Foundation.h> + +int main() +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [pool release]; + return 0; +} diff --git a/Tests/Objective-C/simple-build-test/CMakeLists.txt b/Tests/Objective-C/simple-build-test/CMakeLists.txt new file mode 100644 index 0000000..5ab46ac --- /dev/null +++ b/Tests/Objective-C/simple-build-test/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.15) + +set(CMAKE_MACOSX_RPATH OFF) + +project(simple-build-test OBJC) + +add_library(foo SHARED foo.m) +target_link_libraries(foo "-framework Foundation") + +add_executable(simple-build-test main.m) +target_link_libraries(simple-build-test "-framework Foundation" foo) diff --git a/Tests/Objective-C/simple-build-test/foo.h b/Tests/Objective-C/simple-build-test/foo.h new file mode 100644 index 0000000..b3fb084 --- /dev/null +++ b/Tests/Objective-C/simple-build-test/foo.h @@ -0,0 +1,9 @@ +#import <Foundation/Foundation.h> + +@interface Foo : NSObject { + NSNumber* age; +} + +@property (nonatomic, retain) NSNumber* age; + +@end diff --git a/Tests/Objective-C/simple-build-test/foo.m b/Tests/Objective-C/simple-build-test/foo.m new file mode 100644 index 0000000..2d452a8 --- /dev/null +++ b/Tests/Objective-C/simple-build-test/foo.m @@ -0,0 +1,7 @@ +#import "foo.h" + +@implementation Foo + +@synthesize age; + +@end diff --git a/Tests/Objective-C/simple-build-test/main.m b/Tests/Objective-C/simple-build-test/main.m new file mode 100644 index 0000000..970d554 --- /dev/null +++ b/Tests/Objective-C/simple-build-test/main.m @@ -0,0 +1,12 @@ +#import <Foundation/Foundation.h> +#import "foo.h" + +int main(int argc, char **argv) +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + Foo *theFoo = [[Foo alloc] init]; + theFoo.age = [NSNumber numberWithInt:argc]; + NSLog(@"%d\n",[theFoo.age intValue]); + [pool release]; + return 0; +} diff --git a/Tests/TryCompile/CMakeLists.txt b/Tests/TryCompile/CMakeLists.txt index 54e96a2..8fd8090 100644 --- a/Tests/TryCompile/CMakeLists.txt +++ b/Tests/TryCompile/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required (VERSION 2.6) -project(TryCompile) +project(TryCompile OBJC C CXX) macro(TEST_ASSERT value msg) if (NOT ${value}) @@ -313,6 +313,34 @@ if(CMAKE_C_COMPILER_ID STREQUAL "GNU") TEST_ASSERT(C_STRICT_PROTOTYPES "CHECK_C_COMPILER_FLAG failed -Werror -Wstrict-prototypes") endif() +######################################################################### +# +# Test that the CHECK_OBJCC_SOURCE_COMPILES, CHECK_OBJC_SOURCE_RUNS +# macros work. + +include(CheckOBJCSourceCompiles) +include(CheckOBJCSourceRuns) + +CHECK_OBJC_SOURCE_COMPILES("I don't build in Objective-C" OBJC_BUILD_SHOULD_FAIL) +CHECK_OBJC_SOURCE_COMPILES("int main() { return 0; }" SIMPLE_OBJC_BUILD_SHOULD_WORK) + +TEST_FAIL(OBJC_BUILD_SHOULD_FAIL "CHECK_OBJC_SOURCE_COMPILES() succeeded, but should have failed") +TEST_ASSERT(SIMPLE_OBJC_BUILD_SHOULD_WORK "CHECK_OBJC_SOURCE_COMPILES() failed, but should have succeeded") + +set(CMAKE_REQUIRED_LIBRARIES "-framework Foundation") + +CHECK_OBJC_SOURCE_COMPILES("#import <Foundation/Foundation.h>\nint main()\n{\nNSObject *foo;\nreturn 0;\n}\n" OBJC_BUILD_SHOULD_WORK) +CHECK_OBJC_SOURCE_RUNS("int main() { return 2; }" SIMPLE_OBJC_RUN_SHOULD_FAIL) +CHECK_OBJC_SOURCE_RUNS("int main() { return 0; }" SIMPLE_OBJC_RUN_SHOULD_WORK) +CHECK_OBJC_SOURCE_RUNS("#import <Foundation/Foundation.h>\nint main()\n{\nNSObject *foo;\nreturn 2;\n}\n" OBJC_RUN_SHOULD_FAIL) +CHECK_OBJC_SOURCE_RUNS("#import <Foundation/Foundation.h>\nint main()\n{\nNSObject *foo;\nreturn 0;\n}\n" OBJC_RUN_SHOULD_WORK) + +TEST_ASSERT(OBJC_BUILD_SHOULD_WORK "CHECK_OBJC_SOURCE_COMPILES() failed, but should have succeeded") +TEST_FAIL(SIMPLE_OBJC_RUN_SHOULD_FAIL "CHECK_OBJC_SOURC_RUNS() succeeds, but should have failed") +TEST_ASSERT(SIMPLE_OBJC_RUN_SHOULD_WORK "CHECK_OBJC_SOURCE_RUNS() failed, but should have succeeded") +TEST_FAIL(OBJC_RUN_SHOULD_FAIL "CHECK_OBJC_SOURCE_RUNS() succeeds, but should have failed") +TEST_ASSERT(OBJC_RUN_SHOULD_WORK "CHECK_OBJC_SOURCE_RUNS() failed, but should have succeeded") + ####################################################################### # # also test that the check_prototype_definition macro works |