CMake Examples

  • Basic usage
    # 1. Define the prototype dynamic library (prerequisite).
    add_library(opgraph_math SHARED
    )
    # 2. Generate the ES API package (library+wheel package).
    add_es_library_and_whl(
        ES_LINKABLE_AND_ALL_TARGET es_math
        OPP_PROTO_TARGET  opgraph_math
        OUTPUT_PATH       ${CMAKE_BINARY_DIR}/output
    )
    
    # 3. Use the library in your application.
    add_executable(my_app main.cpp)
    target_link_libraries(my_app PRIVATE es_math)

    If the project has an OBJECT/STATIC intermediate target that requires the ES header file at the build phase time, you can additionally run add_dependencies (<Intermediate target> build_es_math) (build_es_*** name varies with the package name) to ensure that the code is generated before the target is compiled. For the final executable or shared libraries, simply link dependencies of es_math to my_app via target_link_libraries(... es_math).

    add_library(my_obj OBJECT foo.cc bar.cc)
    # Ensure that the build_es_math target is executed before my_obj is compiled. This is usually used to generate the dependent header file.
    # For example, the es_math header file is generated by build_es_math.
    add_dependencies(my_obj build_es_math)
    # Pass the search path of the es_math header file to my_obj.
    target_link_libraries(my_obj PRIVATE es_math)
    
    # Create an executable file my_app and use the target file generated in my_obj ($<TARGET_OBJECTS:my_obj>).
    add_executable(my_app $<TARGET_OBJECTS:my_obj>)
    (Optional) Link the dependencies of es_math to my_app.
    target_link_libraries(my_app PRIVATE
            es_math
    )
  • Enable the system to generate only the C/C++ library.
    # Enable the system to generate only the C/C++ library (skip the Python wheel package).
    add_es_library(
        ES_LINKABLE_AND_ALL_TARGET es_math
        OPP_PROTO_TARGET  opgraph_math
        OUTPUT_PATH       ${CMAKE_BINARY_DIR}/output
    )
  • Enable the system to exclude some operators from generation.
    # Enable the system to generate the math TARGET.
    # Enable the system to exclude es_Add_c.h, es_Add.h, es_Add.cpp, and es_Add.py from artifacts.
    add_es_library(
        ES_LINKABLE_AND_ALL_TARGET es_math
        OPP_PROTO_TARGET  opgraph_math
        OUTPUT_PATH       ${CMAKE_BINARY_DIR}/output
        EXCLUDE_OPS       Add
    )
  • Use multiple TARGETs.
    # Enable the system to generate the math TARGET.
    add_es_library_and_whl(
        ES_LINKABLE_AND_ALL_TARGET es_math
        OPP_PROTO_TARGET  opgraph_math
        OUTPUT_PATH       ${CMAKE_BINARY_DIR}/output
    )
    
    # Enable the system to generate the nn TARGET.
    add_es_library_and_whl(
        ES_LINKABLE_AND_ALL_TARGET es_nn
        OPP_PROTO_TARGET  opgraph_nn
        OUTPUT_PATH       ${CMAKE_BINARY_DIR}/output
    )
    
    # Use multiple TARGETs.
    add_executable(my_inference_app main.cpp)
    target_link_libraries(my_inference_app PRIVATE
        es_math
        es_nn
    )
  • Enable multiple TARGETs to share the output directory. All TARGETs can use the same OUTPUT_PATH. Header files are organized in subdirectories by package name. The following is an example:
    add_es_library_and_whl(
        ES_LINKABLE_AND_ALL_TARGET es_math
        OPP_PROTO_TARGET  opgraph_math
        OUTPUT_PATH       ${CMAKE_BINARY_DIR}/output  # shared path
    )
    
    add_es_library_and_whl(
        ES_LINKABLE_AND_ALL_TARGET es_nn
        OPP_PROTO_TARGET  opgraph_nn
        OUTPUT_PATH       ${CMAKE_BINARY_DIR}/output  # shared path
    )
    
    # Output structure:
    # output/
    # ├── include/
    # │   ├── es_math/
    # │   └── es_nn/
    # ├── lib64/
    # │   ├── libes_math.so
    # │   └── libes_nn.so
    # └── whl/
    #     ├── es_math-1.0.0-py3-none-any.whl
    #     └── es_nn-1.0.0-py3-none-any.whl