From 12fa61af82efd46f7681bb2cd3e615b1cefa3c41 Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Sat, 16 Feb 2019 13:04:43 -0800 Subject: [PATCH] cmake: refactor FindSSP.cmake Use a function to not pollute variable scope. Use modern style. Signed-off-by: Rafael Kitover --- cmake/FindSSP.cmake | 114 ++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 67 deletions(-) diff --git a/cmake/FindSSP.cmake b/cmake/FindSSP.cmake index 4b3057a2..f1047be6 100644 --- a/cmake/FindSSP.cmake +++ b/cmake/FindSSP.cmake @@ -6,91 +6,71 @@ # # To use: # -# put a copy into your /CMakeScripts/ +# put a copy into your /cmake/ # # In your main CMakeLists.txt do something like this: # -# IF(WIN32) -# SET(SSP_STATIC ON) -# ENDIF(WIN32) +# if(WIN32) +# set(SSP_STATIC ON) +# endif() # -# FIND_PACKAGE(SSP) +# find_package(SSP) # -# IF(SSP_LIBRARY) -# SET(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} ${SSP_LIBRARY}") -# SET(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE} ${SSP_LIBRARY}") -# ENDIF(SSP_LIBRARY) -# -# BSD 2-Clause License -# -# Copyright (c) 2016, Rafael Kitover -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# * Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# -# * Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# if(SSP_LIBRARY) +# set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} ${SSP_LIBRARY}") +# set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE} ${SSP_LIBRARY}") +# endif() # only do this when compiling with gcc/g++ -IF(NOT (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUC)) - RETURN() -ENDIF() +if(NOT CMAKE_COMPILER_IS_GNUCXX) + return() +endif() -foreach(arg ${CMAKE_C_COMPILER_ARG1} ${CMAKE_C_COMPILER_ARG2} ${CMAKE_C_COMPILER_ARG3} ${CMAKE_C_COMPILER_ARG4} ${CMAKE_C_COMPILER_ARG5} ${CMAKE_C_COMPILER_ARG6} ${CMAKE_C_COMPILER_ARG7} ${CMAKE_C_COMPILER_ARG8} ${CMAKE_C_COMPILER_ARG9}) - string(STRIP ${arg} arg) - set(gcc_args "${gcc_args};${arg}") -endforeach() +function(FindSSP) + if(NOT CMAKE_CXX_COMPILER AND CMAKE_C_COMPILER) + set(CMAKE_CXX_COMPILER ${CMAKE_C_COMPILER}) + endif() -EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} ${gcc_args} --print-prog-name=gcc OUTPUT_VARIABLE GCC_EXECUTABLE OUTPUT_STRIP_TRAILING_WHITESPACE) + foreach(arg ${CMAKE_CXX_COMPILER_ARG1} ${CMAKE_CXX_COMPILER_ARG2} ${CMAKE_CXX_COMPILER_ARG3} ${CMAKE_CXX_COMPILER_ARG4} ${CMAKE_CXX_COMPILER_ARG5} ${CMAKE_CXX_COMPILER_ARG6} ${CMAKE_CXX_COMPILER_ARG7} ${CMAKE_CXX_COMPILER_ARG8} ${CMAKE_CXX_COMPILER_ARG9}) + string(STRIP ${arg} arg) + set(gcc_args "${gcc_args};${arg}") + endforeach() -execute_process(COMMAND sh -c "command -v ${GCC_EXECUTABLE}" OUTPUT_VARIABLE GCC_EXECUTABLE OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${gcc_args} --print-prog-name=gcc OUTPUT_VARIABLE GCC_EXECUTABLE OUTPUT_STRIP_TRAILING_WHITESPACE) -GET_FILENAME_COMPONENT(GCC_DIRNAME ${GCC_EXECUTABLE} DIRECTORY) + if(WIN32 AND NOT MSYS) + execute_process(COMMAND where.exe ${GCC_EXECUTABLE} OUTPUT_VARIABLE GCC_EXECUTABLE OUTPUT_STRIP_TRAILING_WHITESPACE) + else() + execute_process(COMMAND sh -c "command -v ${GCC_EXECUTABLE}" OUTPUT_VARIABLE GCC_EXECUTABLE OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() -EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} ${gcc_args} --print-libgcc-file-name OUTPUT_VARIABLE LIBGCC_FILE OUTPUT_STRIP_TRAILING_WHITESPACE) + get_filename_component(GCC_DIRNAME "${GCC_EXECUTABLE}" DIRECTORY) -GET_FILENAME_COMPONENT(LIBGCC_DIRNAME ${LIBGCC_FILE} DIRECTORY) + execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${gcc_args} --print-libgcc-file-name OUTPUT_VARIABLE LIBGCC_FILE OUTPUT_STRIP_TRAILING_WHITESPACE) -SET(SSP_SEARCH_PATHS ${GCC_DIRNAME} ${LIBGCC_DIRNAME}) + get_filename_component(LIBGCC_DIRNAME "${LIBGCC_FILE}" DIRECTORY) -SET(CURRENT_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) + set(SSP_SEARCH_PATHS ${GCC_DIRNAME} ${LIBGCC_DIRNAME}) -IF(SSP_STATIC) - IF(WIN32) - SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a) - ELSE(WIN32) - SET(CMAKE_FIND_LIBRARY_SUFFIXES .a) - ENDIF(WIN32) -ENDIF(SSP_STATIC) + if(SSP_STATIC) + if(WIN32) + set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a) + else() + set(CMAKE_FIND_LIBRARY_SUFFIXES .a) + endif() + endif() -FIND_LIBRARY(SSP_LIBRARY - NAMES ssp libssp - HINTS ${SSP_SEARCH_PATHS} - PATH_SUFFIXES lib64 lib lib/x64 lib/x86 -) + find_library(SSP_LIBRARY + NAMES ssp libssp + HINTS ${SSP_SEARCH_PATHS} + PATH_SUFFIXES lib64 lib lib/x64 lib/x86 + ) -SET(CMAKE_FIND_LIBRARY_SUFFIXES ${CURRENT_FIND_LIBRARY_SUFFIXES}) + set(SSP_LIBRARY PARENT_SCOPE) +endfunction() -FOREACH(VAR GCC_DIRNAME LIBGCC_FILE LIBGCC_DIRNAME SSP_SEARCH_PATHS CURRENT_FIND_LIBRARY_SUFFIXES) - UNSET(${VAR}) -ENDFOREACH() +FindSSP() -INCLUDE(FindPackageHandleStandardArgs) +include(FindPackageHandleStandardArgs) -FIND_PACKAGE_HANDLE_STANDARD_ARGS(SSP REQUIRED_VARS SSP_LIBRARY) +find_package_handle_standard_args(SSP REQUIRED_VARS SSP_LIBRARY)