cmake: refactor FindSSP.cmake

Use a function to not pollute variable scope.

Use modern style.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
This commit is contained in:
Rafael Kitover 2019-02-16 13:04:43 -08:00
parent 5a77d8f4ba
commit 12fa61af82
No known key found for this signature in database
GPG Key ID: 08AB596679D86240
1 changed files with 47 additions and 67 deletions

View File

@ -6,91 +6,71 @@
#
# To use:
#
# put a copy into your <project_root>/CMakeScripts/
# put a copy into your <project_root>/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})
function(FindSSP)
if(NOT CMAKE_CXX_COMPILER AND CMAKE_C_COMPILER)
set(CMAKE_CXX_COMPILER ${CMAKE_C_COMPILER})
endif()
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()
endforeach()
EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} ${gcc_args} --print-prog-name=gcc 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)
execute_process(COMMAND sh -c "command -v ${GCC_EXECUTABLE}" OUTPUT_VARIABLE GCC_EXECUTABLE OUTPUT_STRIP_TRAILING_WHITESPACE)
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()
GET_FILENAME_COMPONENT(GCC_DIRNAME ${GCC_EXECUTABLE} DIRECTORY)
get_filename_component(GCC_DIRNAME "${GCC_EXECUTABLE}" DIRECTORY)
EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} ${gcc_args} --print-libgcc-file-name OUTPUT_VARIABLE LIBGCC_FILE OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${gcc_args} --print-libgcc-file-name OUTPUT_VARIABLE LIBGCC_FILE OUTPUT_STRIP_TRAILING_WHITESPACE)
GET_FILENAME_COMPONENT(LIBGCC_DIRNAME ${LIBGCC_FILE} DIRECTORY)
get_filename_component(LIBGCC_DIRNAME "${LIBGCC_FILE}" DIRECTORY)
SET(SSP_SEARCH_PATHS ${GCC_DIRNAME} ${LIBGCC_DIRNAME})
set(SSP_SEARCH_PATHS ${GCC_DIRNAME} ${LIBGCC_DIRNAME})
SET(CURRENT_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if(SSP_STATIC)
if(WIN32)
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a)
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif()
endif()
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)
FIND_LIBRARY(SSP_LIBRARY
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)