You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.5 KiB

  1. # This program source code file is part of KiCad, a free EDA CAD application.
  2. #
  3. # Copyright (C) 2023 KiCad Developers, see AUTHORS.TXT for contributors.
  4. #
  5. # This program is free software: you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by the
  7. # Free Software Foundation, either version 3 of the License, or (at your
  8. # option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program. If not, see <http://www.gnu.org/licenses/>.
  17. # Search paths for protoc when generating code
  18. set( Protobuf_IMPORT_DIRS ${Protobuf_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/proto )
  19. set( KIAPI_PROTO_SRCS
  20. common/envelope.proto
  21. common/types/base_types.proto
  22. common/types/enums.proto
  23. common/types/project_settings.proto
  24. common/commands/base_commands.proto
  25. common/commands/editor_commands.proto
  26. common/commands/project_commands.proto
  27. board/board.proto
  28. board/board_commands.proto
  29. board/board_types.proto
  30. schematic/schematic_types.proto
  31. schematic/schematic_commands.proto
  32. )
  33. # Generated C++ code must be in the build dir; it is dependent on the version of protoc installed
  34. set( KIAPI_CPP_BASEPATH ${CMAKE_CURRENT_BINARY_DIR}/cpp/api )
  35. foreach( PROTO_SRC ${KIAPI_PROTO_SRCS} )
  36. string( REGEX REPLACE "\.proto$" ".pb.cc" CPP_SRC ${PROTO_SRC} )
  37. string( REGEX REPLACE "\.proto$" ".pb.h" CPP_HEADER ${PROTO_SRC} )
  38. set( KIAPI_CPP_SRCS ${KIAPI_CPP_SRCS} ${KIAPI_CPP_BASEPATH}/${CPP_SRC} )
  39. set( KIAPI_CPP_HEADERS ${KIAPI_CPP_HEADERS} ${KIAPI_CPP_BASEPATH}/${CPP_HEADER} )
  40. set( KIAPI_PROTO_SRC_FULLPATHS ${KIAPI_PROTO_SRC_FULLPATHS} ${CMAKE_CURRENT_SOURCE_DIR}/proto/${PROTO_SRC} )
  41. endforeach ()
  42. add_custom_command( COMMAND ${CMAKE_COMMAND} -E make_directory ${KIAPI_CPP_BASEPATH}
  43. COMMAND ${Protobuf_PROTOC_EXECUTABLE}
  44. --cpp_out=dllexport_decl=KIAPI_IMPORTEXPORT:${KIAPI_CPP_BASEPATH}
  45. --proto_path=${CMAKE_CURRENT_SOURCE_DIR}/proto
  46. --experimental_allow_proto3_optional
  47. ${KIAPI_PROTO_SRCS}
  48. COMMENT "Generating API protobuf source files from proto definitions..."
  49. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  50. DEPENDS ${KIAPI_PROTO_SRC_FULLPATHS}
  51. OUTPUT ${KIAPI_CPP_SRCS} ${KIAPI_CPP_HEADERS}
  52. )
  53. # kiapi must be a shared DLL because the protobuf messages can only be initialized once
  54. add_library( kiapi SHARED
  55. ${CMAKE_CURRENT_SOURCE_DIR}/../include/import_export.h
  56. ${KIAPI_CPP_SRCS}
  57. ${KIAPI_CPP_HEADERS}
  58. )
  59. target_compile_definitions( kiapi PRIVATE KIAPI_IMPORTEXPORT=APIEXPORT )
  60. target_compile_definitions( kiapi INTERFACE KIAPI_IMPORTEXPORT=APIIMPORT )
  61. # https://groups.google.com/g/protobuf/c/PDR1bqRazts
  62. if(MSVC)
  63. target_compile_options( kiapi PRIVATE /FI${CMAKE_CURRENT_SOURCE_DIR}/../include/import_export.h )
  64. else()
  65. add_definitions( -include ${CMAKE_CURRENT_SOURCE_DIR}/../include/import_export.h )
  66. endif()
  67. if( APPLE )
  68. # puts library into the main kicad.app bundle in build tree
  69. set_target_properties( kiapi PROPERTIES
  70. LIBRARY_OUTPUT_DIRECTORY "${OSX_BUNDLE_BUILD_LIB_DIR}"
  71. INSTALL_NAME_DIR "${OSX_BUNDLE_BUILD_LIB_DIR}"
  72. )
  73. endif()
  74. install( TARGETS
  75. kiapi
  76. RUNTIME DESTINATION ${KICAD_LIB}
  77. LIBRARY DESTINATION ${KICAD_LIB}
  78. COMPONENT binary
  79. )
  80. if( KICAD_WIN32_INSTALL_PDBS )
  81. # Get the PDBs to copy over for MSVC
  82. install(FILES $<TARGET_PDB_FILE:kiapi> DESTINATION ${KICAD_BIN})
  83. endif()
  84. # Because CMake doesn't guess this from the .cc extension generated by protoc
  85. set_target_properties( kiapi PROPERTIES LINKER_LANGUAGE CXX )
  86. include( ${KICAD_CMAKE_MODULE_PATH}/KiCadVersion.cmake )
  87. # Extract the major and minor build version as a string
  88. string( REGEX MATCH
  89. "([0-9]+)\\.([0-9]+)\\.([0-9]+)"
  90. KICAD_MAJOR_MINOR_PATCH_VERSION
  91. "${KICAD_VERSION}"
  92. )
  93. set_target_properties( kiapi PROPERTIES SOVERSION ${KICAD_MAJOR_MINOR_PATCH_VERSION} )
  94. target_include_directories( kiapi SYSTEM PUBLIC ${Protobuf_INCLUDE_DIRS} )
  95. target_link_libraries( kiapi protobuf::libprotobuf )
  96. target_include_directories( kiapi INTERFACE
  97. ${CMAKE_CURRENT_BINARY_DIR}/cpp # Leaving off the /api/ to make #include statments less ambiguous
  98. )
  99. # Because when building internally, the generated files do not include the "api" base path
  100. target_include_directories( kiapi PUBLIC ${KIAPI_CPP_BASEPATH} )
  101. if( APPLE )
  102. # puts library into the main kicad.app bundle in build tree
  103. set_target_properties( kiapi PROPERTIES
  104. LIBRARY_OUTPUT_DIRECTORY "${OSX_BUNDLE_BUILD_LIB_DIR}"
  105. INSTALL_NAME_DIR "${OSX_BUNDLE_BUILD_LIB_DIR}"
  106. )
  107. set_target_properties( kiapi PROPERTIES INSTALL_RPATH
  108. "@executable_path/../Frameworks" )
  109. set_target_properties( kiapi PROPERTIES BUILD_WITH_INSTALL_RPATH 1 )
  110. endif()
  111. if( NOT (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR} ) )
  112. file( GLOB SCHEMA_FILES ${CMAKE_CURRENT_SOURCE_DIR}/schemas/*.json )
  113. add_custom_target( api_schema_build_copy ALL
  114. COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/schemas ${CMAKE_BINARY_DIR}/schemas
  115. DEPENDS ${SCHEMA_FILES}
  116. COMMENT "Copying API schema files into build directory"
  117. )
  118. endif()
  119. INSTALL( DIRECTORY
  120. schemas
  121. DESTINATION ${KICAD_DATA}
  122. COMPONENT Runtime
  123. )