-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Pass c99 to compiler #3641
Pass c99 to compiler #3641
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a few questions.
CMakeLists.txt
Outdated
@@ -154,6 +154,10 @@ string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") | |||
|
|||
include(CheckCCompilerFlag) | |||
|
|||
if (NOT CMAKE_VERSION VERSION_LESS 3.1.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CMAKE_C_STANDARD
is available from cmake 3.1
ref: https://cmake.org/cmake/help/v3.1/release/3.1.0.html
CMakeLists.txt
Outdated
@@ -175,6 +179,9 @@ if(CMAKE_COMPILER_IS_GNU) | |||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-signedness") | |||
endif() | |||
endif() | |||
if (NOT CMAKE_C_STANDARD AND GCC_VERSION VERSION_LESS 5.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about to always set flag, whatever the compiler version. For later versions as it is their default it shouldn't matter and if at some point their default move to c11 we are still good.
Otherwise why not doing the same for the arm compiler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For later versions as it is their default it shouldn't matter and if at some point their default move to c11 we are still good.
agree.
Otherwise why not doing the same for the arm compiler?
if cmake version < 3.1, there is no portable approach to set -std=c99 for all compiler, we should do it by different compiler.
I add flags for gcc only currently.
BTY, what compiler should we support?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not doing the same for the arm compiler?
I don't think Cmake has any support for Arm compilers. And it shouldn't try to invoke an Arm Compiler unless explicitly requested, because that could cause an unexpected network access to check the license. People who use a “specialist” compiler like Arm Compiler generally have their build environment set up to pass target-specific flags anyway, so Cmake defaults aren't particularly useful for them. The Cmake defaults are mostly useful for non-cross builds where the user just wants to build without fuss.
Makefile
Outdated
@@ -1,5 +1,8 @@ | |||
DESTDIR=/usr/local | |||
PREFIX=mbedtls_ | |||
override CFLAGS+=-std=gnu99 | |||
MAKEOVERRIDES += CFLAGS+=-std=gnu99 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this ok with ARM compiler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No (CI confirms), and it's not ok with many other compilers either. Our makefile should be compiler-agnostic, at least if you pass a different WARNING_CFLAGS
argument. I can't think of a case where an override
in the makefile would be acceptable: when it comes to compiler flags, the makefile suggests, but the user decides.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If _GNU_SOURCE
or some such is needed to support a library feature, the #define
needs to go in the appropriate .c
or .h
file. If the code is using a GNU extension to the C language, it shouldn't, or if the feature is optional (e.g. extra safety or optimization), it should be guarded by conditional compilation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-std=c99
and -std=gnu99
are made by C_EXTENSIONS
in cmake.
and CMAKE_C_EXTENSIONS
is ON
default.
so we need set it to OFF
when cmake?
we should set different flag for different compiler also.
but I have no idea about how do it in Makefile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing -std=c99
when you know that the compiler supports it (GCC, Clang, maybe other compilers with a gcc-compatible command line) is ok. But don't pass it if you don't know. And don't pass -std=gnu99
: our code shouldn't require any GNU extension.
The makefiles are designed to be portable as long as you have GNU make. At least make lib
should work on any OS with any compiler that accepts the options -o
and -c
. They don't even assume Unix utilities, and targets like make clean
that assume Unix utilities (such as rm
) have a Windows alternative (del
). I don't think it's realistic to check the compiler in the makefiles. Note that cross-compilation is very common.
It's ok if some compilers require passing different flags when compiling, for example passing make WARNING_CFLAGS="-Wno-something"
or make CFLAGS="-mfoo -fno-weird-optimization"
. We expect the default to work on the platforms that people use the most (at least when not cross-compiling), but we know we can't cover them all. It's not ok if the makefile passes -std=c99
to the compiler whether the user wanted it or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so we shouldn't add c99 flag in Makefile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so we shouldn't add c99 flag in Makefile?
Right. Unless you find a way to do so that does the right thing for compilers that don't support -std=
, is OS-agnostic, and doesn't make people who aren't GNU make experts want to gouge their eyes out (GNU make is a difficult language once you get past the basics).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have revert changes in Makefile
I current add c99 flag for gcc/clang in Makefile/CMake. is there any other compiler that we need support? |
In CMake: no, it's fine. Other compilers should still be usable, but they don't need to be usable out of the box: it's ok if they require explicit |
CMakeLists.txt
Outdated
@@ -154,6 +154,11 @@ string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") | |||
|
|||
include(CheckCCompilerFlag) | |||
|
|||
if (NOT CMAKE_VERSION VERSION_LESS 3.1.0) | |||
set(CMAKE_C_STANDARD 99) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a problem to not use this at all (remove those 4 lines) and to do "set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")" unconditionally for GNU and ARM compiler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add -std=c99
to CMAKE_C_FLAGS
for GNU and ARM compiler is simple.
but CMAKE_C_STANDARD
and CMAKE_C_EXTENSIONS
is standard variables in CMake.
although they are exists after CMake 3.1.0
adding -std=c99
and some other compiler-specific script should be removed eventually after we drop support for old CMake version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't followed all the conversation so I'm not sure if this is relevant. For information, we try to support maintained versions of Ubuntu and CentOS, which currently means CMake back to 2.8.12.2. And armcc doesn't support -std
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I am convinced with your arguments, let's keep the setting of CMAKE_C_STANDARD. Rather than:
if (NOT CMAKE_VERSION VERSION_LESS 3.1.0), I would prefer though:
if (CMAKE_VERSION VERSION_GREATER ... ).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need a expression cmake >= 3.1.0
.
but VERSION_GREATER_EQUAL
cmake introduced in cmake 3.7.
so we have to use not NOT CMAKE_VERSION VERSION_LESS 3.1.0
Otherwise in the CI the all.sh test component "test_everest: build: Everest ECDH context (ASan build)" is falling. Here is the part of the CI logs reporting the error: [ 14%] Building C object library/CMakeFiles/mbedcrypto.dir/sha1.c.o library/CMakeFiles/mbedcrypto.dir/build.make:1670: recipe for target 'library/CMakeFiles/mbedcrypto.dir//3rdparty/everest/library/Hacl_Curve25519_joined.c.o' failed |
|
The Everest code is less portable than the rest of the library. If it needs |
I added |
may i know when this fix expected to be part of the master? |
@@ -18,6 +18,12 @@ | |||
* | |||
* This file is part of mbed TLS (https://tls.mbed.org) | |||
*/ | |||
#ifndef _BSD_SOURCE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For unusual things like this it is useful to have a comment that explains why they are needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err, yes, but why? what happens if you don't have it?
Fixes Mbed-TLS#3631 Signed-off-by: okhowang(王沛文) <okhowang@tencent.com>
Disable compiler-specific extensions (e.g. use --std=c11 instead of --std=gnu11). Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
@@ -73,9 +73,10 @@ int main( void ) | |||
#endif | |||
#endif /* _MSC_VER */ | |||
#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ | |||
#if defined(MBEDTLS_HAVE_TIME) | |||
#if defined(MBEDTLS_HAVE_TIME) || (defined(MBEDTLS_TIMING_C) && !defined(MBEDTLS_TIMING_ALT)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndrzejKurek LOL :)
(@daverodgman don't worry, just flash-backs to a previous PR!)
@@ -18,6 +18,12 @@ | |||
* | |||
* This file is part of mbed TLS (https://tls.mbed.org) | |||
*/ | |||
#ifndef _BSD_SOURCE | |||
#define _BSD_SOURCE /* Required to enable compilation under --std=c99 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So actually I think this should be
#define _BSD_SOURCE /* Required to enable compilation under --std=c99 */ | |
/* Required to get htole64() from gcc/glibc's endian.h (older systems) | |
* when we compile with -std=c99 */ | |
#define _BSD_SOURCE |
#define _BSD_SOURCE /* Required to enable compilation under --std=c99 */ | ||
#endif | ||
#ifndef _DEFAULT_SOURCE | ||
#define _DEFAULT_SOURCE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#define _DEFAULT_SOURCE | |
/* (modern version of _BSD_SOURCE) */ | |
#define _DEFAULT_SOURCE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://stackoverflow.com/a/29201732/7761803 (not clear that _BSD_SOURCE
is still required, but let's continue to support older systems)
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@davidhorstmann-arm can you re-review please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - thanks!
CI failure is spurious |
Fixes #3631
Signed-off-by: okhowang(王沛文) okhowang@tencent.com
Notes:
Signed-off-by:
line from the committer to certify that the contribution is made under the terms of the Developer Certificate of Origin.Description
Pass standard c99 to compiler
Status
READY
Requires Backporting
No
Migrations
NO
Additional comments
Todos
Steps to test or reproduce