Skip to content

Duplicate compiler flags on first CMake invocation #323

Closed

Description

@kkaefer

I'm using NDK r14, but have observed this behavior in earlier NDKs as well: When generating the build directory with CMake, the first invocation adds the Android-specific compiler flags twice. E.g. when compiling for armeabiv7-a, I'm seeing two sets of these flags:

-g
-DANDROID
-ffunction-sections
-funwind-tables
-fstack-protector-strong
-no-canonical-prefixes
-march=armv7-a
-mfloat-abi=softfp
-mfpu=vfpv3-d16
-fno-integrated-as
-mthumb
-Wa,--noexecstack
-Wformat
-Werror=format-security
-std=c++11
-frtti
-fexceptions

While this doesn't make the build fail, a subsequent rerun of CMake (e.g. triggered by a changed CMakeLists.txt file) will rebuild the ninja file to only include one set of these flags. This will in turn trigger a full recompile of all sources, since the compile flags changed. On larger project, this is undesirable since it takes a long time.

The error happens because CMake invokes the toolchain file twice during the first build, but only once during subsequent build system rebuilds.

This can be solved by adding an include guard at the top of the toolchain file:

if(ANDROID_NDK_TOOLCHAIN_INCLUDED)
	return()
endif(ANDROID_NDK_TOOLCHAIN_INCLUDED)
set(ANDROID_NDK_TOOLCHAIN_INCLUDED true)

Activity

  1. modified the milestones: unplanned, r15 on Mar 13, 2017
  2. DanAlbert commented on Mar 13, 2017

    @DanAlbert
    Contributor

    Given how temperamental cmake toolchain files seem to be, I'm going to get this into r15 beta 1 to try to flush out any issues early.

  3. self-assigned this
    on Mar 13, 2017
  4. hrydgard commented on Mar 13, 2017

    @hrydgard

    Possibly related: When a build fails with an error in Android Studio the first time after having changed CMakeLists or its parameters, it immediately starts another gradle sync and wipes out the build log from the Gradle Console window, which is pretty annoying.

  5. DanAlbert commented on Mar 13, 2017

    @DanAlbert
    Contributor

    Does that happen even with the snippet to fix this is added? If so it's probably something we should raise with the Studio team.

  6. hrydgard commented on Mar 13, 2017

    @hrydgard

    Actually, still happens with the snipped fix added, so it's an unrelated issue.

    Repro:

    1. Cause a compiler error, for example insert #error hello anywhere in your C++ code
    2. build (click the hammer button). wait for it to error.
    3. Change your CMakelists.txt (say, add message("hello")
    4. build again

    After step 4, gradle will immediately wipe the build log when the build error happens and start a sync, as I mentioned above, requiring another click on the build button to see the error text.

    Guess I should go file that over at https://code.google.com/p/android/issues/entry?template=Tools%20bug%20report ?

  7. DanAlbert commented on Mar 15, 2017

    @DanAlbert
    Contributor

    @hrydgard: Yeah, they should be able to help you.

    https://android-review.googlesource.com/c/353248/ to fix spurious rebuilds in r15 beta 1. Thanks for the fix, @kkaefer!

  8. added a commit that references this issue on Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions