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

Do not define AVIF_ENABLE_NODISCARD for avif.h #2351

Conversation

wantehchang
Copy link
Collaborator

@wantehchang wantehchang commented Jul 31, 2024

Make the AVIF_ENABLE_NODISCARD cmake option require C23 so that avif.h
can define the AVIF_NODISCARD macro as [[nodiscard]] without checking
the AVIF_ENABLE_NODISCARD macro. This requires changing avif.h to test
__STDC_VERSION__ >= 202000L, which is the value of __STDC_VERSION__ in
recent versions of GCC and Clang when -std=c2x or -std=gnu2x is
specified.

In general avif.h should only test compiler predefined macros. The only
exception is AVIF_DLL (ignoring the experimental feature macros.)

Fix https://github.com/AOMediaCodec/libavif/issues/2352.

@wantehchang
Copy link
Collaborator Author

@vrabaud FYI.

CMakeLists.txt Outdated
@@ -33,8 +33,16 @@ include(FetchContent)
include(FindPkgConfig)
include(AvifExternalProjectUtils)

option(AVIF_ENABLE_NODISCARD "Add [[nodiscard]] to some functions. CMake must be at least 3.21 to request C23." OFF)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is copied from line 55 in the original code, with "force C23" changed to "request C23."

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.21.0)
set(CMAKE_C_STANDARD 23)
set_property(TARGET avif_obj PROPERTY C_STANDARD 23)
else()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we redefine AVIF_ENABLE_NODISCARD to be a request rather than a requirement, we don't need this else block.

target_compile_options(avif_obj PUBLIC $<BUILD_INTERFACE:$<$<COMPILE_LANGUAGE:C>:/std:clatest>>)
endif()
endif()
target_compile_definitions(avif_obj PUBLIC $<BUILD_INTERFACE:AVIF_ENABLE_NODISCARD=1>)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The definition of the AVIF_ENABLE_NODISCARD macro for avif.h is also deleted.

Copy link
Collaborator

@jzern jzern left a comment

Choose a reason for hiding this comment

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

This makes sense.

The decaying is unfortunate:

https://cmake.org/cmake/help/latest/manual/cmake-compile-features.7.html#id8

But it looks like CMAKE_C_STANDARD_REQUIRED might work if we wanted to abort when the option was specified, but the version not available.

@wantehchang wantehchang force-pushed the make-AVIF_ENABLE_NODISCARD-best-effort branch from 42c32ab to d64aa4d Compare August 1, 2024 21:28
Redefine the AVIF_ENABLE_NODISCARD cmake option to request rather than
require the AVIF_NODISCARD macro be defined as [[nodiscard]]. There are
two reasons:

1. The CMAKE_C_STANDARD variable merely requests the given version of
   the C standard. If the compiler doesn't support the requested version
   of the C standard, it "decays" to a previous version of the C
   standard. Making avif.h use [[nodiscard]] in this case will result in
   compilation errors.
2. In general avif.h should only test compiler predefined macros. The
   only exception is AVIF_DLL (ignoring the experimental feature
   macros.)
@wantehchang wantehchang force-pushed the make-AVIF_ENABLE_NODISCARD-best-effort branch from d64aa4d to 0c616d2 Compare August 1, 2024 21:47
@wantehchang wantehchang force-pushed the make-AVIF_ENABLE_NODISCARD-best-effort branch from 0c616d2 to 35c4a4e Compare August 1, 2024 21:53
@wantehchang
Copy link
Collaborator Author

But it looks like CMAKE_C_STANDARD_REQUIRED might work if we wanted to abort when the option was specified, but the version not available.

Thank you for the suggestion. I implemented this in the second commit. I also rewrote the commit message and pasted it at the top of this issue. Please take a look. Thanks!

@wantehchang wantehchang requested a review from jzern August 1, 2024 21:57
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
else()
set(CMAKE_C_STANDARD 99)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would think this would be required too, but it's highly unlikely anyone would try this with a c90 only compiler (or force those flags)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, we assume the compiler supports C99.

@wantehchang wantehchang merged commit 93b4ad4 into AOMediaCodec:main Aug 2, 2024
32 checks passed
@wantehchang wantehchang deleted the make-AVIF_ENABLE_NODISCARD-best-effort branch August 2, 2024 01:32
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