Last time I talked about how to integrate NASM in a CMake/Clang build, but if you are developing on a Mac you may want to use Xcode to debug your assembly code. Xcode needs a bit of massaging in order to support NASM within a C/C project. Getting the latest NASM First I suggest you install the latest version of NASM using Homebrew like so.
- Set (CMAKEASMNASMSOURCEFILEEXTENSIONS nasm asm) if (NOT CMAKEASMNASMOBJECTFORMAT) if (WIN32) if (DEFINED CMAKECSIZEOFDATAPTR AND.
- My bad experiences with CMake NASM. Back in December I wrote about how to make CMake behave nicely with NASM. I remember wasting quite some time digging through CMake’s source trying to figure out a solution on how to have CMake detect NASM correctly. This has since been fixed in recent versions of CMake. Custom preprocessor.
- CMake NASM Test This project builds a hello world console application in x86-64 assembly with NASM using CMake.
- Revert 'analysis-save/load: Enable reuse of cutree info in reuse-levels = 2' This reverts commit a82c6c7a7d5f5ef88a37c6a443e0fe.
annotate source/cmake/CMakeASM_NASMInformation.cmake @ 12722:cd98d17c690cdraft
date | Fri, 22 Jan 2021 08:26:58 +0530 |
---|
rev | line source |
---|---|
11943 ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 1 set(ASM_DIALECT '_NASM') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 2 set(CMAKE_ASM${ASM_DIALECT}_SOURCE_FILE_EXTENSIONS asm) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 3 |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 4 if(X64) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 5 list(APPEND ASM_FLAGS -DARCH_X86_64=1 -I ${CMAKE_CURRENT_SOURCE_DIR}/./common/x86/) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 6 if(ENABLE_PIC) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 7 list(APPEND ASM_FLAGS -DPIC) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 8 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 9 if(APPLE) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 10 set(ARGS -f macho64 -DPREFIX) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 11 elseif(UNIX AND NOT CYGWIN) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 12 set(ARGS -f elf64) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 13 else() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 14 set(ARGS -f win64) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 15 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 16 else() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 17 list(APPEND ASM_FLAGS -DARCH_X86_64=0 -I ${CMAKE_CURRENT_SOURCE_DIR}/./common/x86/) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 18 if(APPLE) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 19 set(ARGS -f macho32 -DPREFIX) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 20 elseif(UNIX AND NOT CYGWIN) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 21 set(ARGS -f elf32) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 22 else() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 23 set(ARGS -f win32 -DPREFIX) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 24 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 25 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 26 |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 27 if(GCC) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 28 list(APPEND ASM_FLAGS -DHAVE_ALIGNED_STACK=1) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 29 else() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 30 list(APPEND ASM_FLAGS -DHAVE_ALIGNED_STACK=0) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 31 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 32 |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 33 if(HIGH_BIT_DEPTH) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 34 if(MAIN12) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 35 list(APPEND ASM_FLAGS -DHIGH_BIT_DEPTH=1 -DBIT_DEPTH=12 -DX265_NS=${X265_NS}) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 36 else() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 37 list(APPEND ASM_FLAGS -DHIGH_BIT_DEPTH=1 -DBIT_DEPTH=10 -DX265_NS=${X265_NS}) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 38 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 39 else() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 40 list(APPEND ASM_FLAGS -DHIGH_BIT_DEPTH=0 -DBIT_DEPTH=8 -DX265_NS=${X265_NS}) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 41 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 42 |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 43 list(APPEND ASM_FLAGS '${CMAKE_ASM_NASM_FLAGS}') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 44 |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 45 if(CMAKE_BUILD_TYPE MATCHES Release) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 46 list(APPEND ASM_FLAGS '${CMAKE_ASM_NASM_FLAGS_RELEASE}') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 47 elseif(CMAKE_BUILD_TYPE MATCHES Debug) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 48 list(APPEND ASM_FLAGS '${CMAKE_ASM_NASM_FLAGS_DEBUG}') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 49 elseif(CMAKE_BUILD_TYPE MATCHES MinSizeRel) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 50 list(APPEND ASM_FLAGS '${CMAKE_ASM_NASM_FLAGS_MINSIZEREL}') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 51 elseif(CMAKE_BUILD_TYPE MATCHES RelWithDebInfo) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 52 list(APPEND ASM_FLAGS '${CMAKE_ASM_NASM_FLAGS_RELWITHDEBINFO}') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 53 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 54 |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 55 set(NASM_FLAGS ${ARGS} ${ASM_FLAGS} PARENT_SCOPE) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 56 string(REPLACE ';' ' ' CMAKE_ASM_NASM_COMPILER_ARG1 '${ARGS}') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 57 |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 58 # This section exists to override the one in CMakeASMInformation.cmake |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 59 # (the default Information file). This removes the <FLAGS> |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 60 # thing so that your C compiler flags that have been set via |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 61 # set_target_properties don't get passed to nasm and confuse it. |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 62 if(NOT CMAKE_ASM${ASM_DIALECT}_COMPILE_OBJECT) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 63 string(REPLACE ';' ' ' STR_ASM_FLAGS '${ASM_FLAGS}') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 64 set(CMAKE_ASM${ASM_DIALECT}_COMPILE_OBJECT '<CMAKE_ASM${ASM_DIALECT}_COMPILER> ${STR_ASM_FLAGS} -o <OBJECT> <SOURCE>') |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 65 endif() |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 66 |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 67 include(CMakeASMInformation) |
ac3fda7e760cx86: Change assembler from YASM to NASM Vignesh Vijayakumar <[email protected]> diffchangeset | 68 set(ASM_DIALECT) |
The Android NDK supports using CMake tocompile C and C++ code for your application. This page discusses how to useCMake with the NDK via the Android Gradle Plugin's
Note: If you are using Android Studio, go to Add C and C++ code to yourproject to learn the basics of adding native sources to your project, creatinga CMake build script, adding your CMake project as a Gradle dependency, andusing newer versions of CMake than those included in the SDK.ExternalNativeBuild
or wheninvoking CMake directly.The CMake toolchain file
The NDK supports CMake via a toolchain file. Toolchain files are CMake filesthat customize the behavior of the toolchain for cross-compiling. The toolchainfile used for the NDK is located in the NDK at
Note: If the Android SDK is installed, then the NDK is installed in the SDKdirectory in <NDK>/build/cmake/android.toolchain.cmake
.ndk/version/
or ndk-bundle/
.Build parameters such as ABI,
Warning: CMake has its own built-in NDK support. This workflowis not supported by Android and is often broken with new NDK releases or whenusing older versions of CMake. Use of this workflow is strongly discouraged, andwill not work with Gradle. In the future these two workflows will merge and bothbe supported by Android. See Issue 463 for more information.minSdkVersion
, etc. Plex in italiano. are given on the commandline when invoking cmake
. For a list of supported arguments, see theToolchain arguments section.Usage
Gradle
Use of the CMake toolchain file is automatic when using
externalNativeBuild
. See Android Studio's Add C and C++ code to yourproject guide for more information.Command Line
When building with CMake outside of Gradle, the toolchain file itself andits arguments must be passed to CMake. For example:
Toolchain arguments
The following arguments can be passed to the CMake toolchain file. If buildingwith Gradle, add arguments to
Note: Any required arguments are passed automatically by Gradle and need only bepassed explicitly if building from the command line.android.defaultConfig.externalNativeBuild.cmake.arguments
as described in theExternalNativeBuild docs. If building from the command line, pass arguments toCMake with -D
. For example, to force armeabi-v7a to always build with Neonsupport, pass -DANDROID_ARM_NEON=TRUE
.ANDROID_ABI
Note: This is a required argument.The target ABI. For information on supported ABIs, see Android ABIs.
Gradle
Gradle provides this argument automatically. Do not explicitly set thisargument in your
build.gradle
file. To control what ABIs Gradle targets,use abiFilters
Github desktop azure devops login. as described in Android ABIs.Command Line
CMake builds for a single target per build. To target more than one AndroidABI, you must build once per ABI. It is recommended to use different builddirectories for each ABI to avoid collisions between builds.
Value | Notes |
---|---|
armeabi-v7a | |
armeabi-v7a with NEON | Same as -DANDROID_ABI=armeabi-v7a -DANDROID_ARM_NEON=ON . |
arm64-v8a | |
x86 | |
x86_64 |
ANDROID_ARM_MODE
Specifies whether to generate arm or thumb instructions for armeabi-v7a. Has noeffect for other ABIs. For more information, see the Android ABIsdocumentation.
Value | Notes |
---|---|
arm | |
thumb | Default behavior. |
ANDROID_ARM_NEON
Enables or disables NEON for armeabi-v7a. Has no effect for other ABIs. Defaultsto true for API level (
minSdkVersion
or ANDROID_PLATFORM
) 23 or newer, falseotherwise. For more information, see the Neon support documentation.Value | Notes |
---|---|
TRUE | Default for API level 23 or newer. |
FALSE | Default for API level 22 or older. |
ANDROID_LD
Selects which linker to use. lld is currently experimental for the NDK and canbe enabled with this argument.
Value | Notes |
---|---|
lld | Enables lld. |
default | Use the default linker for the given ABI. |
ANDROID_NATIVE_API_LEVEL
Alias for ANDROID_PLATFORM.
ANDROID_PLATFORM
Specifies the minimum API level supported by the application or library. Thisvalue corresponds to the application's
minSdkVersion
.Gradle
When using the Android Gradle Plugin, this value is automatically set tomatch the application's
minSdkVersion
and should not be set manually.Command Line
When invoking CMake directly, this value defaults to the lowest API levelsupported by the NDK in use. For example, with NDK r20 this value defaultsto API level 16.
Warning: NDK libraries cannot be run on devices with an API level below theANDROID_PLATFORM
value with which the code was built.Multiple formats are accepted for this parameter:
android-$API_LEVEL
$API_LEVEL
android-$API_LETTER
The
$API_LETTER
format allows you to specify android-N
without the need todetermine the number associated with that release. Note that some releasesreceived an API increase without a letter increase. These APIs can be specifiedby appending the -MR1
suffix. For example, API level 25 is android-N-MR1
.ANDROID_STL
Specifies which STL to use for this application. For more information, see C++library support. By default,
Caution: The default behavior is not appropriate for all applications. Be sureto read the C++ library support guide and in particular the section aboutstatic runtimes before choosing an STL.c++_static
will be used.Value | Notes |
---|---|
c++_shared | The shared library variant of libc++. |
c++_static | The static library variant of libc++. |
none | No C++ standard library suport. |
system | The system STL |
Understand the CMake build command
When debugging CMake build issues, it's helpful to know the specific buildarguments that Gradle uses when cross-compiling for Android.
The Fujifilm X-Pro2 is a radical reworking of the first X-Series mirrorless interchangeable lens camera. It may look essentially the same but just about every aspect of the camera has been refreshed, extended and enhanced. Read our full review to find out what these changes amount to.
The Android Gradle Plugin saves the build arguments it uses for executing aCMake build for each ABI and build typepair to the
Note: Older versions of the Android Gradle Plugin placed these files in thecmake_build_command.txt
. These files are found in the followingdirectory:.externalNativeBuild
directory rather than the .cxx
directory.The following snippet shows an example of the CMake arguments to build adebuggable release of the
hello-jni
sample targeting the armeabi-v7a
architecture.Use prebuilt libraries
For instructions on using prebuilt libraries with CMake, see the
add_library
documentation regarding IMPORTED
targets in the CMake manual.YASM support in CMake
The NDK provides CMake support for building assembly code written inYASM to run on x86 and x86-64architectures. YASM is an open-source assembler for x86 and x86-64architectures, based on the NASM assembler.
To build assembly code with CMake, make the following changes in your project's
CMakeLists.txt
:Cmake Nasm Login
- Call
enable_language
with the value set toASM_NASM
. - Depending on whether you are building a shared library or an executablebinary, call
add_library
oradd_executable
. Inthe arguments, pass in a list of source files consisting of the.asm
filesfor the assembly program in YASM and the.c
files for the associated Clibraries or functions.
The following snippet shows how you might configure your
CMakeLists.txt
tobuild a YASM program as a shared library.For an example of how to build a YASM program as an executable, see the yasmtest in the NDK git repository.
Report problems
Cmake_asm_nasm_compiler Could Be Found
If you run into any issues with the NDK or its CMake toolchain file, report themvia the android-ndk/ndk issue tracker on GitHub.