We are catching errors in our CMake makefiles due to lack of -fPIC
. Her's one from a ci20 MIPS dev-board:
...
[ 92%] Built target cryptopp-object
Scanning dependencies of target cryptopp-shared
Scanning dependencies of target cryptopp-static
Linking CXX static library libcryptopp.a
Linking CXX shared library libcryptopp.so
/usr/bin/ld: CMakeFiles/cryptopp-object.dir/cryptlib.cpp.o: relocation R_MIPS_HI16 against
`a local symbol' can not be used when making a shared object; recompile with -fPIC
CMakeFiles/cryptopp-object.dir/cryptlib.cpp.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
The project's policy is to us PIC everywhere except 32-bit x86 due to register pressures. That means x86_64, ARM-32, Aarch32, Aarch64, MIPS, MIPS64, UltraSparc, etc get PIC.
I believe the target processor is provided in CMAKE_SYSTEM_PROCESSOR
. The problem I am having is the docs don't tell me the values, so I can't figure out how to craft a "not 32-bit x86" test.
How do I detect 32-bit x86 processor in CMakeList.txt?
Even better, I would like to see a comprehensive list of processors that CMake sets CMAKE_SYSTEM_PROCESSOR
to. If anyone has the list, then it would be great to provide it.
I believe this performs the detection on nearly everything except Windows. Windows does not consume
-fPIC
, so it does not matter to me. The pieces were glued together from three Stack Overflow answers.You get the machine with
uname -m
, and its mostly accurate, even on OS X. For example, on OS X,uname -p
returnsi386
whileuname -m
returnsx86_64
. I seem to recall 10.6 or 10.7 was a little flaky as the transition was being made to 64-bit Macs.You get the processor with
uname -p
sometimes, but it fails on many dev-boards. For example my ci20 dev-board returns "mips" for the machine and "unknown" for the processor. Another example is my LeMaker HiKey. It returns "aarch64" for the machine and "unknown" for the processor.I'd still like to see the list of processors provided by Cmake.
I probably would build something around the compiler.
A close approximation using existing variables/modules would be:
In short what I have done:
WIN32
is also valid for 64 Bit Windows compilers/environmentsCMAKE_SIZEOF_VOID_P GREATER 4
checks for "greater then 32 Bit"CMAKE_POSITION_INDEPENDENT_CODE
to set-fPIC
I admit a more accurate method would be to build something around a pre-defined macros test.
Edit: Added "Predefined Macros Check" Alternative
Here is the more precise check for predefined macros:
References