From 2af2fe42a3c02982a9f7714522d6457acbec6a3d Mon Sep 17 00:00:00 2001 From: DRC Date: Mon, 5 Dec 2016 16:52:54 -0600 Subject: [PATCH] Build: Clean up inline keyword detection Strict C89-conformant compilers don't support the "inline" keyword, but most of them support "__inline__", and that keyword can be used with the always_inline atribute as well. This commit also removes duplicate code by using a foreach() loop to test the various keywords. --- CMakeLists.txt | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c57a243..b0cd72e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -420,37 +420,31 @@ if(UNIX) endif() endif() +if(MSVC) + set(INLINE_OPTIONS "__inline;inline") +else() + set(INLINE_OPTIONS "__inline__;inline") +endif() option(FORCE_INLINE "Force function inlining" TRUE) boolean_number(FORCE_INLINE) if(FORCE_INLINE) if(MSVC) - set(INLINE "__forceinline") - check_c_source_compiles("__forceinline void foo(void) {} int main(void) { foo(); }" - FORCE_INLINE_WORKS) + list(INSERT INLINE_OPTIONS 0 "__forceinline") else() - set(INLINE "inline __attribute__((always_inline))") - check_c_source_compiles("static inline __attribute__((always_inline)) void foo(void) {} int main(void) { foo(); }" - FORCE_INLINE_WORKS) + list(INSERT INLINE_OPTIONS 0 "inline __attribute__((always_inline))") + list(INSERT INLINE_OPTIONS 0 "__inline__ __attribute__((always_inline))") endif() endif() -if(NOT FORCE_INLINE OR NOT FORCE_INLINE_WORKS) - if(MSVC) - set(INLINE "__inline") - check_c_source_compiles("__inline void foo(void) {} int main(void) { foo(); }" - INLINE_WORKS) - else() - set(INLINE "__inline__") - check_c_source_compiles("static __inline__ void foo(void) {} int main(void) { foo(); }" - INLINE_WORKS) - endif() - if(NOT INLINE_WORKS) - set(INLINE "inline") - check_c_source_compiles("static inline void foo(void) {} int main(void) { foo(); }" - C99_INLINE_WORKS) - if(NOT C99_INLINE_WORKS) - message(FATAL_ERROR "Could not determine how to inline functions.") - endif() +foreach(inline ${INLINE_OPTIONS}) + check_c_source_compiles("${inline} static void foo(void) {} int main(void) { foo(); }" + INLINE_WORKS) + if(INLINE_WORKS) + set(INLINE ${inline}) + break() endif() +endforeach() +if(NOT INLINE_WORKS) + message(FATAL_ERROR "Could not determine how to inline functions.") endif() message(STATUS "INLINE = ${INLINE} (FORCE_INLINE = ${FORCE_INLINE})") -- 2.40.0