Compilation Using CMake

In a project, you can use CMake to more easily use the BiSheng Compiler to compile Ascend C operators and generate executable files, dynamic libraries, static libraries, or binary files.

The following is an example of the CMake script and its core steps:
# 1. find_package(ASC) is a command used in CMake to search for and configure the Ascend C compilation toolchain.
find_package(ASC)  

# 2. Specify that the project supports the ASC and CXX languages. ASC indicates that the Ascend C programming language can be compiled using the BiSheng Compiler.
project(kernel_samples LANGUAGES ASC CXX)

# 3. Use the CMake API to compile executable files, dynamic libraries, static libraries, and binary files.
add_executable(demo
    add_custom.asc
)
#.....
target_compile_options(demo PRIVATE
    # --npu-arch specifies the NPU architecture version. dav- is followed by the architecture version number. For details about the architecture version number of each product model, see Table 1.
    # <COMPILE_LANGUAGE:ASC>: indicates that the compilation option takes effect only for the ASC language.
    $<$<COMPILE_LANGUAGE:ASC>: --npu-arch=dav-2201>    
)

The following is an example of compiling a dynamic library and a static library, and shows how to switch the source file to be compiled using the ASC language.

  • Compile the .cpp file to generate a dynamic library.
    # Set the .cpp file to the ASC attribute and enable the Ascend C language for compilation.
    set_source_files_properties(
        add_custom_base.cpp 
        sub_custom_base.cpp
        PROPERTIES LANGUAGE ASC
    )
    
    add_library(kernel_lib SHARED
        add_custom_base.cpp 
        sub_custom_base.cpp
    )
    
    add_executable(demo
        main.cpp
    )
    target_compile_definitions(demo PRIVATE
        ASCENDC_DUMP=0
    )
    target_compile_options(demo PRIVATE
        -g
    )
    target_include_directories(demo PRIVATE
        include
    )
    target_link_libraries(demo PRIVATE
        kernel_lib
    )
  • Compile the .asc file to generate a static library.
    # By default, the .asc file is compiled using the Ascend C language. You do not need to use set_source_files_properties to set the language.
    add_library(kernel_lib STATIC
        add_custom_base.asc 
        sub_custom_base.asc
    )
    
    add_executable(demo
        main.cpp
    )
    target_compile_definitions(demo PRIVATE
        ASCENDC_DUMP=0
    )
    target_compile_options(demo PRIVATE
        -g
    )
    target_include_directories(demo PRIVATE
        include
    )
    target_link_libraries(demo PRIVATE
        kernel_lib
    )

The following lists the common and default link libraries used during CMake compilation.

Table 1 Common link libraries (When using high-level APIs, you must link the following libraries because they are required for the functionality of high-level APIs. In other scenarios, you can choose whether to link these libraries as needed.)

Name

Description

Use Case

libtiling_api.a

Library related to the tiling function.

This library needs to be linked when the tiling API related to high-level APIs is used.

libregister.so

Library related to tiling registration.

This library needs to be linked when the tiling API related to high-level APIs is used.

libgraph_base.so

Basic data structures and API library.

This library needs to be linked when basic structures such as ge::Shape and ge::DataType are called.

libplatform.so

Hardware platform information library.

This library needs to be linked when the hardware platform information API related to PlatformAscendC is used.

Table 2 Default link libraries

Name

Description

libascendc_runtime.a

Assembly library for Ascend C operator parameters.

libruntime.so

Runtime library.

libprofapi.so

Library for collecting running performance data of Ascend C operators.

libascendalog.so

CANN log collection library.

libmmpa.so

CANN system API library.

libascend_dump.so

CANN maintenance and test information library.

libc_sec.so

CANN security function library.

liberror_manager.so

CANN error information management library.

libascendcl.so

ACL-related API library.