rewritten findfreepascal, moved checkstack code elsewhere
authorkoda
Wed, 06 Mar 2013 11:18:54 +0100
changeset 8665 3ff8694d9e22
parent 8664 6204ac0293b4
child 8666 1652c1d9adc8
rewritten findfreepascal, moved checkstack code elsewhere
CMakeLists.txt
cmake_modules/FindFreePascal.cmake
cmake_modules/FindFreepascal.cmake
hedgewars/CMakeLists.txt
--- a/CMakeLists.txt	Wed Mar 06 10:13:23 2013 +0100
+++ b/CMakeLists.txt	Wed Mar 06 11:18:54 2013 +0100
@@ -230,6 +230,17 @@
     set(CMAKE_CXX_FLAGS_DEBUG "-Wall -DDEBUG")
 endif()
 
+#TODO: find out why we need this...
+include(CheckCSourceCompiles)
+set(CMAKE_REQUIRED_FLAG "-Wl,-z -Wl,noexecstack")
+check_c_source_compiles("int main(void) { return 0; }" HAVE_NOEXECSTACK)
+if(HAVE_NOEXECSTACK)
+    list(APPEND pascal_flags "-k-z" "-knoexecstack")
+    if(NOT ${MINIMAL_FLAGS})
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAG}")
+    endif()
+endif()
+unset(CMAKE_REQUIRED_FLAG)
 
 #parse additional parameters
 if(FPFLAGS OR GHFLAGS)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake_modules/FindFreePascal.cmake	Wed Mar 06 11:18:54 2013 +0100
@@ -0,0 +1,37 @@
+# - Try to find the FreePascal executable
+# Once done this will define
+#
+#  FREEPASCAL_FOUND       - system has Freepascal
+#  FREEPASCAL_VERSION     - Freepascal version
+#  FREEPASCAL_EXECUTABLE  - Freepascal executable
+#
+# Copyright (c) 2012, Bryan Dunsmore <dunsmoreb@gmail.com>
+# Copyright (c) 2013, Vittorio Giovara <vittorio.giovara@gmail.com>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+
+find_program(FREEPASCAL_EXECUTABLE
+    NAMES fpc
+    PATHS /opt/local/bin /usr/local/bin /usr/bin
+    )
+
+if (FREEPASCAL_EXECUTABLE)
+    # check Freepascal version
+    execute_process(COMMAND ${FREEPASCAL_EXECUTABLE} -iV
+                    OUTPUT_VARIABLE FREEPASCAL_VERSION
+                    ERROR_VARIABLE FREEPASCAL_VERSION_ERROR
+                    RESULT_VARIABLE FREEPASCAL_VERSION_RESULT
+                    OUTPUT_STRIP_TRAILING_WHITESPACE
+                    )
+
+    if(NOT ${FREEPASCAL_VERSION_RESULT} EQUAL 0)
+        message(SEND_ERROR "Command \"${FREEPASCAL_EXECUTABLE} -iV\" failed with output: ${FREEPASCAL_VERSION_ERROR}")
+    endif()
+endif()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(FreePascal DEFAULT_MSG FREEPASCAL_EXECUTABLE FREEPASCAL_VERSION)
+mark_as_advanced(FREEPASCAL_VERSION)
+
--- a/cmake_modules/FindFreepascal.cmake	Wed Mar 06 10:13:23 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-# Load Freepascal
-if (FPC)
-    set(FPC_EXECUTABLE ${FPC})
-else()
-    find_program(FPC_EXECUTABLE
-        NAMES fpc
-        PATHS /opt/local/bin /usr/local/bin /usr/bin)
-endif()
-
-# Check Freepascal version
-if (FPC_EXECUTABLE)
-    exec_program(${FPC_EXECUTABLE} ARGS "-v" OUTPUT_VARIABLE FPC_VERSION_FULL)
-
-    string(REGEX MATCH "[0-9]+\\.[0-9]+" FPC_VERSION_LONG "${FPC_VERSION_FULL}")
-    string(REGEX REPLACE "([0-9]+\\.[0-9]+)" "\\1" FPC_VERSION "${FPC_VERSION_LONG}")
-    message(STATUS "Found Freepascal: ${FPC_EXECUTABLE} (version ${FPC_VERSION})")
-else()
-    message(FATAL_ERROR "Could NOT find Freepascal")
-endif()
-
-# Check for noexecstack flag support
-message(STATUS "Checking whether linker needs explicit noexecstack")
-set(NOEXECSTACK_FLAGS "-k-z" "-knoexecstack")
-file(WRITE ${EXECUTABLE_OUTPUT_PATH}/checkstack.pas "begin end.")
-
-execute_process(COMMAND ${FPC_EXECUTABLE} ${NOEXECSTACK_FLAGS} checkstack.pas
-    WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
-    RESULT_VARIABLE TEST_NOEXECSTACK
-    OUTPUT_QUIET ERROR_QUIET)
-
-if (TEST_NOEXECSTACK)
-    set(NOEXECSTACK_FLAGS "")
-    message(STATUS "Checking whether linker needs explicit noexecstack -- no")
-else(TEST_NOEXECSTACK)
-    message(STATUS "Checking whether linker needs explicit noexecstack -- yes")
-endif(TEST_NOEXECSTACK)
-
--- a/hedgewars/CMakeLists.txt	Wed Mar 06 10:13:23 2013 +0100
+++ b/hedgewars/CMakeLists.txt	Wed Mar 06 11:18:54 2013 +0100
@@ -120,11 +120,10 @@
 endif(${BUILD_ENGINE_LIBRARY})
 
 
-# Check Freepascal version
-find_package(Freepascal)
-
-if (FPC_VERSION VERSION_LESS required_fpc_version)
-    message(FATAL_ERROR "Freepascal is too old, minimum version required is ${required_fpc_version}")
+find_package(FreePascal REQUIRED)
+#when cmake-2.6 support is dropped, this ought to be inside FindFreePascal.cmake
+if (FREEPASCAL_VERSION VERSION_LESS required_fpc_version)
+    message(FATAL_ERROR "Freepascal ${FREEPASCAL_VERSION} is too old, minimum version required is ${required_fpc_version}")
 endif()
 
 
@@ -179,7 +178,7 @@
 
 
 #this command is a workaround to some inlining issues present in older FreePascal versions and fixed in 2.6
-if(FPC_VERSION VERSION_LESS "2.6")
+if(FREEPASCAL_VERSION VERSION_LESS "2.6")
     #under some configurations CMAKE_BUILD_TOOL fails to pass on the jobserver, breaking parallel compilation
     if(UNIX)
         set(SAFE_BUILD_TOOL $(MAKE))
@@ -217,12 +216,12 @@
 endif()
 
 
-set(fpc_flags ${NOEXECSTACK_FLAGS} ${pascal_flags} ${hwengine_project})
+set(fpc_flags ${pascal_flags} ${hwengine_project})
 
 if(NOT APPLE)
     #here is the command for standard executables or for shared library
     add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/${engine_output_name}"
-        COMMAND "${FPC_EXECUTABLE}"
+        COMMAND "${FREEPASCAL_EXECUTABLE}"
         ARGS ${fpc_flags} -o${engine_output_name}
         MAIN_DEPENDENCY ${hwengine_project}
         DEPENDS ${engine_sources}
@@ -232,7 +231,7 @@
     foreach (build_arch ${powerpc_build} ${i386_build} ${x86_64_build})
         list(APPEND lipo_args_list "${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}")
         add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/hwengine.${build_arch}"
-            COMMAND "${FPC_EXECUTABLE}"
+            COMMAND "${FREEPASCAL_EXECUTABLE}"
             ARGS ${fpc_flags} -ohwengine.${build_arch} -P${build_arch}
             MAIN_DEPENDENCY ${hwengine_project}
             DEPENDS ${engine_sources}
@@ -274,7 +273,7 @@
 endif()
 
 #this command is a workaround to some inlining issues present in older FreePascal versions and fixed in 2.6
-if((FPC_VERSION VERSION_LESS "2.6") AND (NOVIDEOREC OR NOT ${FFMPEG_FOUND}))
+if((FREEPASCAL_VERSION VERSION_LESS "2.6") AND (NOVIDEOREC OR NOT ${FFMPEG_FOUND}))
     add_dependencies(hwengine ENGINECLEAN)
 endif()