XNSim/XNInterfaceGenServer/CMakeListsGen.cpp

92 lines
3.7 KiB
C++
Raw Permalink Normal View History

#include "CMakeListsGen.h"
bool CMakeListsGen::generateCMakeLists(const AllInterfaceData &interfaceData,
const std::string &idlPath, const std::string &configName)
{
std::string idlDirPath = fs::path(idlPath).parent_path().string();
std::string cmakefilePath = idlDirPath + "/CMakeLists.txt";
// 1. 生成CMakeLists.txt文件
std::ofstream cmakeFile(cmakefilePath);
if (!cmakeFile.is_open()) {
return false;
}
cmakeFile << "cmake_minimum_required(VERSION 3.16)" << std::endl;
cmakeFile << "project(" << configName << "_Interface LANGUAGES CXX)" << std::endl;
cmakeFile << "set(CMAKE_CXX_STANDARD 17)" << std::endl;
cmakeFile << "set(CMAKE_CXX_STANDARD_REQUIRED ON)" << std::endl;
cmakeFile << "set(CMAKE_POSITION_INDEPENDENT_CODE ON)" << std::endl;
// 获取环境变量
cmakeFile << "if(DEFINED ENV{XNCore})" << std::endl;
cmakeFile << "\tset(XNCore_PATH $ENV{XNCore})" << std::endl;
cmakeFile << "else()" << std::endl;
cmakeFile << "\tmessage(FATAL_ERROR \"Environment variable XNCore is not set.\")" << std::endl;
cmakeFile << "endif()" << std::endl;
// 添加 XNCore_PATH 下的 include 目录为包含目录
cmakeFile << "include_directories(${XNCore_PATH}/include)" << std::endl;
cmakeFile << "if(NOT fastcdr_FOUND)" << std::endl;
cmakeFile << "\tfind_package(fastcdr 2 REQUIRED)" << std::endl;
cmakeFile << "endif()" << std::endl;
cmakeFile << "if(NOT fastdds_FOUND)" << std::endl;
cmakeFile << "\tfind_package(fastdds 3 REQUIRED)" << std::endl;
cmakeFile << "endif()" << std::endl;
cmakeFile << "find_package(OpenSSL REQUIRED)" << std::endl;
cmakeFile << "add_library(" << configName << "_Interface SHARED" << std::endl;
cmakeFile << " " << configName << ".hpp" << std::endl;
cmakeFile << " " << configName << "CdrAux.hpp" << std::endl;
cmakeFile << " " << configName << "CdrAux.ipp" << std::endl;
cmakeFile << " " << configName << "PubSubTypes.hpp" << std::endl;
cmakeFile << " " << configName << "PubSubTypes.cxx" << std::endl;
cmakeFile << " " << configName << "TypeObjectSupport.hpp" << std::endl;
cmakeFile << " " << configName << "TypeObjectSupport.cxx" << std::endl;
for (const auto &ataInterfaceData : interfaceData.ataInterfaceData) {
std::string ataName = ataInterfaceData.ataName;
for (const auto &structInterfaceData : ataInterfaceData.structInterfaceData) {
std::string structName = structInterfaceData.modelStructName;
cmakeFile << " " << ataName << "/" << structName << ".hpp" << std::endl;
cmakeFile << " " << ataName << "/" << structName << ".cxx" << std::endl;
}
}
cmakeFile << ")" << std::endl;
cmakeFile << "target_link_libraries(" << configName << "_Interface PRIVATE" << std::endl;
cmakeFile << " fastcdr fastdds OpenSSL::SSL OpenSSL::Crypto" << std::endl;
cmakeFile << " ${XNCore_PATH}/lib/libXNCore.so" << std::endl;
cmakeFile << ")" << std::endl;
cmakeFile << "target_compile_definitions(" << configName << "_Interface PRIVATE "
<< toUpper(configName) << "_INTERFACE_LIBRARY)" << std::endl;
cmakeFile << "if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)" << std::endl;
cmakeFile
<< "\tset(CMAKE_INSTALL_PREFIX \"${XNCore_PATH}\" CACHE PATH \"Install path prefix\" FORCE)"
<< std::endl;
cmakeFile << "endif()" << std::endl;
cmakeFile << "include(GNUInstallDirs)" << std::endl;
cmakeFile << "install(TARGETS " << configName << "_Interface" << std::endl;
cmakeFile << " RUNTIME DESTINATION ." << std::endl;
cmakeFile << " LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}" << std::endl;
cmakeFile << " ARCHIVE DESTINATION ." << std::endl;
cmakeFile << ")" << std::endl;
cmakeFile.close();
return true;
}
std::string CMakeListsGen::toUpper(const std::string &str)
{
std::string result = str;
for (auto &c : result) {
c = std::toupper(c);
}
return result;
}