# This program source code file is part of KiCad, a free EDA CAD application.
#
# Copyright (C) 2023 KiCad Developers, see AUTHORS.TXT for contributors.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see .
# Search paths for protoc when generating code
set( Protobuf_IMPORT_DIRS ${Protobuf_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/proto )
set( KIAPI_PROTO_SRCS
common/envelope.proto
common/types/base_types.proto
common/commands/base_commands.proto
common/commands/editor_commands.proto
board/board_types.proto
)
# Generated C++ code must be in the build dir; it is dependent on the version of protoc installed
set( KIAPI_CPP_BASEPATH ${CMAKE_CURRENT_BINARY_DIR}/cpp/api )
foreach( PROTO_SRC ${KIAPI_PROTO_SRCS} )
string( REGEX REPLACE "\.proto$" ".pb.cc" CPP_SRC ${PROTO_SRC} )
string( REGEX REPLACE "\.proto$" ".pb.h" CPP_HEADER ${PROTO_SRC} )
set( KIAPI_CPP_SRCS ${KIAPI_CPP_SRCS} ${KIAPI_CPP_BASEPATH}/${CPP_SRC} )
set( KIAPI_CPP_HEADERS ${KIAPI_CPP_HEADERS} ${KIAPI_CPP_BASEPATH}/${CPP_HEADER} )
set( KIAPI_PROTO_SRC_FULLPATHS ${KIAPI_PROTO_SRC_FULLPATHS} ${CMAKE_CURRENT_SOURCE_DIR}/proto/${PROTO_SRC} )
endforeach ()
add_custom_command( COMMAND ${CMAKE_COMMAND} -E make_directory ${KIAPI_CPP_BASEPATH}
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
--cpp_out=dllexport_decl=KIAPI_IMPORTEXPORT:${KIAPI_CPP_BASEPATH}
--proto_path=${CMAKE_CURRENT_SOURCE_DIR}/proto
${KIAPI_PROTO_SRCS}
COMMENT "Generating API protobuf source files from proto definitions..."
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${KIAPI_PROTO_SRC_FULLPATHS}
OUTPUT ${KIAPI_CPP_SRCS} ${KIAPI_CPP_HEADERS}
)
# kiapi must be a shared DLL because the protobuf messages can only be initialized once
add_library( kiapi SHARED
${CMAKE_CURRENT_SOURCE_DIR}/../include/import_export.h
${KIAPI_CPP_SRCS}
${KIAPI_CPP_HEADERS}
)
target_compile_definitions( kiapi PRIVATE KIAPI_IMPORTEXPORT=APIEXPORT )
target_compile_definitions( kiapi INTERFACE KIAPI_IMPORTEXPORT=APIIMPORT )
# https://groups.google.com/g/protobuf/c/PDR1bqRazts
if(MSVC)
target_compile_options( kiapi PRIVATE /FI${CMAKE_CURRENT_SOURCE_DIR}/../include/import_export.h )
else()
add_definitions( -include ${CMAKE_CURRENT_SOURCE_DIR}/../include/import_export.h )
endif()
if( APPLE )
# puts library into the main kicad.app bundle in build tree
set_target_properties( kiapi PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${OSX_BUNDLE_BUILD_LIB_DIR}"
INSTALL_NAME_DIR "${OSX_BUNDLE_BUILD_LIB_DIR}"
)
endif()
install( TARGETS
kiapi
RUNTIME DESTINATION ${KICAD_LIB}
LIBRARY DESTINATION ${KICAD_LIB}
COMPONENT binary
)
if( KICAD_WIN32_INSTALL_PDBS )
# Get the PDBs to copy over for MSVC
install(FILES $ DESTINATION ${KICAD_BIN})
endif()
# Because CMake doesn't guess this from the .cc extension generated by protoc
set_target_properties( kiapi PROPERTIES LINKER_LANGUAGE CXX )
target_include_directories( kiapi SYSTEM PUBLIC ${Protobuf_INCLUDE_DIRS} )
target_link_libraries( kiapi protobuf::libprotobuf )
target_include_directories( kiapi INTERFACE
${CMAKE_CURRENT_BINARY_DIR}/cpp # Leaving off the /api/ to make #include statments less ambiguous
)
# Because when building internally, the generated files do not include the "api" base path
target_include_directories( kiapi PUBLIC ${KIAPI_CPP_BASEPATH} )
option( KICAD_BUILD_ENUM_EXPORTER
"Build the enum exporter used as part of generating the IPC APIs"
OFF )
if( KICAD_BUILD_ENUM_EXPORTER )
add_subdirectory( enums )
add_custom_target( enum_definitions
COMMAND $ ${CMAKE_CURRENT_BINARY_DIR}/enums.json
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generating API definitions from KiCad enums..."
DEPENDS enum_exporter
)
endif()