St.Antario
St.Antario

Reputation: 27425

CMake how to set nasm file extenstions

I'm trying to compile nasm source files with CMake and have some misunderstanding about suffixes. By default CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS has value nasm;asm so I wanted *.S to be recognized as ASM_NASM files by CMake. Setting

set(CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS "${CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS};S")

does not seem to work. Here is the full CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(casm)
enable_language(ASM_NASM)
set(CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS "${CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS};S")
set(CMAKE_ASM_NASM_COMPILE_OPTIONS "${CMAKE_ASM_NASM_COMPILE_OPTIONS} -f elf64 -g")
add_executable(min main.c min.S)

which prints the following error:

CMake Error: CMake can not determine linker language for target: min
CMake Error: Cannot determine link language for target "min".

While simply renaming the min.S to min.asm and replacing adding executable with

add_executable(min main.c min.asm)

works just fine:

Scanning dependencies of target min
[ 33%] Building C object CMakeFiles/min.dir/main.c.o
[ 66%] Building ASM_NASM object CMakeFiles/min.dir/min.asm.o
[100%] Linking C executable min
[100%] Built target min

I also tried to set(CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS "") without any effect so is user settings to the property simply ignored?

Upvotes: 0

Views: 2113

Answers (2)

Dmitry
Dmitry

Reputation: 823

I faced the same issue, with the '.s' extension. This works fine:

set_source_files_properties(min.S PROPERTIES LANGUAGE ASM_NASM)

More general:

file(GLOB_RECURSE src_asm "<path>/*.S" "<path>/*.s")
foreach(X IN ITEMS ${src_asm})
    set_source_files_properties(${X} PROPERTIES LANGUAGE ASM_NASM)
endforeach()
target_sources(target PRIVATE ${src_c} ${src_asm})

Upvotes: 3

St.Antario
St.Antario

Reputation: 27425

I think this is sort of a bug. I use CMake 3.10.2 and I noticed that the variable is not being applied.

After running the CMake tool it produces output files in the build directory like

build_dir/CMakeFiles/3.10.2/
      |
      |__CMakeASM_NASMCompiler.cmake
      |
      |__CMakeCXXCompiler.cmake
      |
      |
     ...

The CMakeASM_NASMCompiler.cmake looks as:

set(CMAKE_ASM_NASM_COMPILER "/usr/bin/nasm")
set(CMAKE_ASM_NASM_COMPILER_ARG1 "")
set(CMAKE_AR "/usr/bin/ar")
set(CMAKE_ASM_NASM_COMPILER_AR "")
set(CMAKE_RANLIB "/usr/bin/ranlib")
set(CMAKE_ASM_NASM_COMPILER_RANLIB "")
set(CMAKE_LINKER "/usr/bin/ld")
set(CMAKE_ASM_NASM_COMPILER_LOADED 1)
set(CMAKE_ASM_NASM_COMPILER_ID "NASM")
set(CMAKE_ASM_NASM_COMPILER_VERSION "")
set(CMAKE_ASM_NASM_COMPILER_ENV_VAR "ASM_NASM")


set(CMAKE_ASM_NASM_IGNORE_EXTENSIONS h;H;o;O;obj;OBJ;def;DEF;rc;RC)
set(CMAKE_ASM_NASM_LINKER_PREFERENCE 0)

As can be seen the variable CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS is not appeared in the file (even if it is set explicitly in the CMakeLists.txt).

After adding the entry

set(CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS "asm;nasm;S")

into the file and regenerating Makefile the .S-files assembles just fine.

Upvotes: 0

Related Questions