Skip to content

CMakeLists.txt: Guard for static_assert - #3320

Open
Dave-Allured wants to merge 1 commit into
AOMediaCodec:mainfrom
Dave-Allured:patch-1
Open

CMakeLists.txt: Guard for static_assert#3320
Dave-Allured wants to merge 1 commit into
AOMediaCodec:mainfrom
Dave-Allured:patch-1

Conversation

@Dave-Allured

Copy link
Copy Markdown

I am not experienced with CMake. Please adjust or reposition this patch, as appropriate. Thanks.

Comment thread CMakeLists.txt
# Require static_assert support in function context, as added in 1.4.2.
# Was missing in some older C11 compilers: Apple Clang <= 7.0.2, etc.
# https://github.com/AOMediaCodec/libavif/issues/3319
target_compile_features(avif PRIVATE c_static_assert)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @vrabaud

Dave: Thank you for the pull request. We can try the following simple change:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c9a7f82d..f29d4b39 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -51,10 +51,10 @@ option(AVIF_ENABLE_NODISCARD "Add [[nodiscard]] to some functions." OFF)
 if(AVIF_ENABLE_NODISCARD)
     # [[nodiscard]] requires C23.
     set(CMAKE_C_STANDARD 23)
-    set(CMAKE_C_STANDARD_REQUIRED ON)
 else()
     set(CMAKE_C_STANDARD 11)
 endif()
+set(CMAKE_C_STANDARD_REQUIRED ON)
 
 # SOVERSION scheme: MAJOR.MINOR.PATCH
 #   If there was an incompatible interface change:

If we want to avoid setting CMAKE_C_STANDARD globally, I would prefer to use c_std_11 instead of c_static_assert because we may use other C11 features such as alignas in the future. Also, we would need to call target_compile_features(... PRIVATE c_std_11) on all the C targets, not just avif.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wantehchang I understand your concerns. I think you should avoid setting CMAKE_C_STANDARD globally, only for this purpose. I am concerned about possible wider disruption, which seems unnecessary right now. My intent was a very minor improvement in diagnostic messaging for legacy compilers. I was looking for a way to set c_static_assert globally, but I did not find this in CMake documentation.

Let's cancel this PR and not disturb the code base. I have a viable solution for the problem that I raised in #3319.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dave: I will look into avoiding setting CMAKE_C_STANDARD globally. I believe target_compile_features() is the recommended method:

https://cmake.org/cmake/help/latest/manual/cmake-compile-features.7.html#requiring-language-standards

I need to figure out a way to use either c_std_11 or c_std_23 depending on whether AVIF_ENABLE_NODISCARD is enabled.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reconsidered this and checked (briefly) the CMake documentation. c_static_assert is a fine-grained approach which targets only the code base in its current state at 1.4.2. Meta-options such as c_std_11 carry more requirements, with a slight risk of disturbing current builds in some contexts. I see this as unnecessary disturbance for new language features which are not yet used or needed.

https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_C_KNOWN_FEATURES.html

Therefore I recommend only two conservative alternatives at this time.

  • Either apply c_static_assert as I originally suggested,
  • Or make no change at this time, wait for new code requirements before adding new compiler requirements.

This addition of CMake c_static_assert is a simple one-line update. You can easily discard or change this at any time in the future, as code requirements change.

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

Successfully merging this pull request may close these issues.

2 participants