Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It's better to determine __STDC_VERSION__ by build system #358

Closed
leleliu008 opened this issue Apr 22, 2023 · 1 comment
Closed

It's better to determine __STDC_VERSION__ by build system #358

leleliu008 opened this issue Apr 22, 2023 · 1 comment

Comments

@leleliu008
Copy link

I noticed that #319 introduced a variable JAS_STDC_VERSION to allow user set a proper value via -DJAS_STDC_VERSION=<VALUE> when cross compiling. the reason is try_run command can not run when cross compiling.

Actually, we do not need to compile a C source code to get the value of __STDC_VERSION__, we just need to run C Preprocesser like following shell code:

cc -std=c11 -E -dM - < /dev/null | sed -n '/__STDC_VERSION__/p' | cut -d ' ' -f3

translate above shell code to cmake script is as follows:

set(STDC_VERSION 0)

execute_process(
    COMMAND ${CMAKE_C_COMPILER} -std=c${CMAKE_C_STANDARD} -E -dM test.c
    OUTPUT_VARIABLE STDC_VERSION
    OUTPUT_STRIP_TRAILING_WHITESPACE
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)

string(REGEX MATCH "#define __STDC_VERSION__ [0-9]+L" STDC_VERSION "${STDC_VERSION}")
string(REPLACE " " ";" STDC_VERSION "${STDC_VERSION}")
list(GET STDC_VERSION 2 STDC_VERSION)

message("STDC_VERSION=${STDC_VERSION}")

the value of __STDC_VERSION__ is determined by -std=, -std= is fixed in CMakeLists.txt, so this value should be calculated by our build system, not by user.

@mdadams
Copy link
Collaborator

mdadams commented Apr 24, 2023

You cannot assume that JasPer is being built on a Linux/Unix platform. Also, you cannot assume that the compiler has a command-line interface similar to GCC/Clang. Many users of JasPer do not use Linux/Unix and/or GCC/Clang. So, "-std=..." will not have the desired effect in many cases. The same goes for the "-E", "-dM", and such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants