Skip to content

Commit

Permalink
plugingenerator: Added option to generate scopy sdk project.
Browse files Browse the repository at this point in the history
You can now generate an out of tree (oot) plugin support project within
plugin generator. To work, the scopy sdk project requires importing the
libraries and dependencies that were generated with cpack.

Signed-off-by: andreidanila1 <[email protected]>
  • Loading branch information
andreidanila1 committed Aug 13, 2024
1 parent 7250c7b commit c7d7a69
Show file tree
Hide file tree
Showing 8 changed files with 854 additions and 8 deletions.
5 changes: 5 additions & 0 deletions tools/plugingenerator/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"sdk":{
"enable": true,
"deps_path":"/home/andrei/git_repositories/scopy2_dev/build-scopy-Desktop_Qt_5_15_2_GCC_64bit-Debug/package",
"project_path": ""
},
"plugin": {
"dir_name": "new",
"plugin_name": "newplugin",
Expand Down
143 changes: 135 additions & 8 deletions tools/plugingenerator/plugin_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
pathToScopy = args.scopy_path
else:
pathToScopy = os.path.dirname(os.path.dirname(os.getcwd()))

pluginsPath = os.path.join(pathToScopy, "plugins")
if not os.path.exists(pluginsPath):
print("Couldn't find " + pluginsPath + " path!")
exit(1)

pathToConfigFile = ""
if args.config_file_path:
Expand All @@ -49,6 +44,7 @@

filesGenerated = []
directoriesGenerated = []
sdkSupport = generatorOptions["sdk"]["enable"]
pluginDirName = generatorOptions["plugin"]["dir_name"]
pluginName = generatorOptions["plugin"]["plugin_name"]
pluginDisplayName = generatorOptions["plugin"]["plugin_display_name"]
Expand All @@ -57,6 +53,32 @@
pluginExportMacro = "SCOPY_" + pluginName.upper() + "_EXPORT"

print("Starting file generation:")

pluginsPath = os.path.join(pathToScopy, "plugins")
if sdkSupport:
sdkPath = generatorOptions["sdk"]["project_path"]
if not sdkPath:
sdkPath = os.path.join(pathToScopy, "ScopySDK")
else:
sdkPath = os.path.join(sdkPath, "ScopySDK")
try:
os.mkdir(sdkPath, mode)
directoriesGenerated.append(sdkPath)
except FileExistsError:
print(sdkPath + " directory already exists!")

pluginsPath = os.path.join(sdkPath, "plugin")
try:
os.mkdir(pluginsPath, mode)
directoriesGenerated.append(pluginsPath)
except FileExistsError:
print(pluginsPath + " directory already exists!")


if not os.path.exists(pluginsPath):
print("Couldn't find " + pluginsPath + " path!")
exit(1)

#################################################### Plugin dir #############################################
newPluginPath = os.path.join(pluginsPath, pluginDirName)
if not os.path.exists(newPluginPath):
Expand Down Expand Up @@ -98,6 +120,107 @@
else:
print(pluginSrcConfigPath + " file already exists!")

####################################################### sdk ##################################################
if sdkSupport:
sdkCmakeFuncFilePath = os.path.join(sdkPath, "SdkSupport.cmake")
if not os.path.exists(sdkCmakeFuncFilePath):
sdkCmakeFuncTemplate = Template(
filename="templates/sdk/sdk_cmake_func_template.mako"
)
sdkCmakeFuncContent = sdkCmakeFuncTemplate.render()
sdkCmakeFuncFile = open(sdkCmakeFuncFilePath, "w")
sdkCmakeFuncFile.write(sdkCmakeFuncContent)
sdkCmakeFuncFile.close()
filesGenerated.append(sdkCmakeFuncFilePath)
else:
print(sdkCmakeFuncFilePath + " file already exists!")

sdkCmakeFilePath = os.path.join(sdkPath, "CMakeLists.txt")
if not os.path.exists(sdkCmakeFilePath):
sdkCmakeTemplate = Template(
filename="templates/sdk/sdk_cmake_template.mako"
)
sdkCmakeContent = sdkCmakeTemplate.render(
deps_path=generatorOptions["sdk"]["deps_path"],
plugin_dir=pluginDirName,
plugin_name=pluginName
)
sdkCmakeFile = open(sdkCmakeFilePath, "w")
sdkCmakeFile.write(sdkCmakeContent)
sdkCmakeFile.close()
filesGenerated.append(sdkCmakeFilePath)
else:
print(sdkCmakeFilePath + " file already exists!")

sdkIncludePath = os.path.join(sdkPath, "include")
try:
os.mkdir(sdkIncludePath, mode)
directoriesGenerated.append(sdkIncludePath)
except FileExistsError:
print(sdkIncludePath + " directory already exists!")

sdkHeaderFilePath = os.path.join(sdkIncludePath, "sdkwindow.h")
if not os.path.exists(sdkHeaderFilePath):
sdkHeaderTemplate = Template(
filename="templates/sdk/sdk_header_template.mako"
)
sdkHeaderContent = sdkHeaderTemplate.render()
sdkHeaderFile = open(sdkHeaderFilePath, "w")
sdkHeaderFile.write(sdkHeaderContent)
sdkHeaderFile.close()
filesGenerated.append(sdkHeaderFilePath)
else:
print(sdkHeaderFilePath + " file already exists!")

sdkCmakeInFilePath = os.path.join(sdkIncludePath, "sdk-util_config.h.cmakein")
if not os.path.exists(sdkCmakeInFilePath):
sdkCmakeInTemplate = Template(
filename="templates/sdk/sdk_cmakein_template.mako"
)
sdkCmakeInTemplate = sdkCmakeInTemplate.render()
sdkCmakeInFile = open(sdkCmakeInFilePath, "w")
sdkCmakeInFile.write(sdkCmakeInTemplate)
sdkCmakeInFile.close()
filesGenerated.append(sdkCmakeInFilePath)
else:
print(sdkCmakeInFilePath + " file already exists!")

sdkSrcPath = os.path.join(sdkPath, "src")
try:
os.mkdir(sdkSrcPath, mode)
directoriesGenerated.append(sdkSrcPath)
except FileExistsError:
print(sdkSrcPath + " directory already exists!")

sdkSrcFilePath = os.path.join(sdkSrcPath, "main.cpp")
if not os.path.exists(sdkSrcFilePath):
sdkSrcTemplate = Template(
filename="templates/sdk/sdk_src_template.mako"
)
sdkSrcContent = sdkSrcTemplate.render()
sdkSrcFile = open(sdkSrcFilePath, "w")
sdkSrcFile.write(sdkSrcContent)
sdkSrcFile.close()
filesGenerated.append(sdkSrcFilePath)

sdkResPath = os.path.join(sdkPath, "res")
try:
os.mkdir(sdkResPath, mode)
directoriesGenerated.append(sdkResPath)
except FileExistsError:
print(sdkResPath + " directory already exists!")
shutil.copy(pathToScopy+"/gui/res/stylesheets/default.qss",sdkResPath)
sdkResQrc = os.path.join(sdkResPath, "resources.qrc")
if not os.path.exists(sdkResQrc):
resFile = open(sdkResQrc, "w")
resFile.write("<RCC>\n")
resFile.write(" <qresource prefix=\"/\">\n")
resFile.write(" <file>default.qss</file>\n")
resFile.write(" </qresource>\n")
resFile.write("</RCC>")
resFile.close()
filesGenerated.append(sdkResQrc)

##################################################### Include ################################################
includePath = os.path.join(newPluginPath, "include")
try:
Expand Down Expand Up @@ -171,8 +294,9 @@
+ str(generatorOptions["test"]["cmake_min_required"])
+ ")\n\n"
)
if generatorOptions["test"]["tst_pluginloader"]:
testCmakeFile.write("include(ScopyTest)\n\nsetup_scopy_tests(pluginloader)")
if not sdkSupport:
if generatorOptions["test"]["tst_pluginloader"]:
testCmakeFile.write("include(ScopyTest)\n\nsetup_scopy_tests(pluginloader)")
filesGenerated.append(testCmakePath)
else:
print(testCmakePath + " file already exists!")
Expand Down Expand Up @@ -214,11 +338,14 @@
print(resQrc + " file already exists!")

##################################################### Plugin CMakeLists #########################################

if generatorOptions["plugin"]["cmakelists"]:
cmakeListsPath = os.path.join(newPluginPath, "CMakeLists.txt")
cmakeTemplate = Template(filename="templates/cmakelists_template.mako")

cmakeContent = cmakeTemplate.render(
scopy_module=pluginName,
sdk_en=sdkSupport,
scopy_module=pluginName,
plugin_display_name=pluginDisplayName,
plugin_description=pluginDecription, config=generatorOptions["cmakelists"]
)
Expand Down
13 changes: 13 additions & 0 deletions tools/plugingenerator/templates/cmakelists_template.mako
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ configure_file(
target_include_directories(${"${PROJECT_NAME}"} INTERFACE ${"${CMAKE_CURRENT_SOURCE_DIR}"}/include)
target_include_directories(${"${PROJECT_NAME}"} PRIVATE ${"${CMAKE_CURRENT_SOURCE_DIR}"}/include/${"${SCOPY_MODULE}"})

% if oot_en:
target_include_directories(${"${PROJECT_NAME}"} PRIVATE ${"${SDK_DEPS_INCLUDE}"})

include(${"${CMAKE_SOURCE_DIR}"}/SdkSupport.cmake)
inlcude_dirs(${"${SDK_DEPS_INCLUDE}"})

target_link_libraries(${"${PROJECT_NAME}"} PUBLIC Qt::Widgets Qt::Core)

link_libs(${"${SDK_DEPS_LIB}"})

% else:
target_include_directories(${"${PROJECT_NAME}"} PUBLIC scopy-pluginbase scopy-gui)

target_link_libraries(
Expand All @@ -76,6 +87,8 @@ target_link_libraries(
scopy-iioutil
)

% endif

if(${"${CMAKE_SYSTEM_NAME}"} MATCHES "Windows")
configureinstallersettings(${"${SCOPY_MODULE}"} ${"${PLUGIN_DESCRIPTION}"} FALSE)
endif()
Expand Down
25 changes: 25 additions & 0 deletions tools/plugingenerator/templates/sdk/sdk_cmake_func_template.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Define a function to include all directories recursively
function(inlcude_dirs root_dir)
# Find all files and directories recursively
file(GLOB_RECURSE all_items LIST_DIRECTORIES true ""${"${root_dir}"}/"")
# Loop through each item found
foreach(item ${"${all_items}"})
# Check if the item is a directory
if(IS_DIRECTORY ${"${item}"})
message(${"${item}"})
target_include_directories(${"${PROJECT_NAME}"} PRIVATE ${"${item}"})
endif()
endforeach()
endfunction()

# Define a function to link all .so files from a root directory
function(link_libs root_dir)
# Find all .so files from root_dir
file(GLOB all_libs "${"${root_dir}"}/*.so")
# Loop through each library found
foreach(lib ${"${all_libs}"})
# Link libraries
message(${"${lib}"})
target_link_libraries(${"${PROJECT_NAME}"} PRIVATE ${"${lib}"})
endforeach()
endfunction()
65 changes: 65 additions & 0 deletions tools/plugingenerator/templates/sdk/sdk_cmake_template.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
cmake_minimum_required(VERSION 3.9)

# Project name
set(TARGET_NAME "ScopySDK")

project(${"${TARGET_NAME}"} VERSION 0.0.1 DESCRIPTION "Project Description")

# Make sure CMake will take care of moc for us
set(CMAKE_AUTOMOC ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SDK_DEPS_PATH ${deps_path})
if(NOT DEFINED SDK_DEPS_PATH)
message(FATAL_ERROR "SDK_DEPS_PATH is required!")
else()
if(NOT EXISTS ${"${SDK_DEPS_PATH}"})
message(FATAL_ERROR "The path=" \"${"${SDK_DEPS_PATH}"}\" " to the dependencies doesn't exist!")
endif()
endif()
set(SDK_DEPS_INCLUDE ${"${SDK_DEPS_PATH}"}/usr/local/include)
if(NOT EXISTS ${"${SDK_DEPS_INCLUDE}"})
message(FATAL_ERROR "The path=" \"${"${SDK_DEPS_INCLUDE}"}\" " to the headers doesn't exist!")
endif()

set(SDK_DEPS_LIB ${"${SDK_DEPS_PATH}"}/usr/local/lib)
if(NOT EXISTS ${"${SDK_DEPS_LIB}"})
message(FATAL_ERROR "The path=" \"${"${SDK_DEPS_LIB}"}\" " to the libraries doesn't exist!")
endif()

set(PLUGIN_INSTALL_PATH ${"${CMAKE_CURRENT_BINARY_DIR}"}/plugin/${plugin_dir}/libscopy-${plugin_name}.so)

find_package(QT NAMES Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${"${QT_VERSION_MAJOR}"} REQUIRED COMPONENTS Widgets Core)

file(GLOB SRC_LIST src/*.cpp)
file(GLOB HEADER_LIST include/*.h include/*.hpp)

configure_file(include/sdk-util_config.h.cmakein ${"${CMAKE_CURRENT_SOURCE_DIR}"}/include/sdk-util_config.h @ONLY)

set(PROJECT_SOURCES ${"${SRC_LIST}"} ${"${HEADER_LIST}"} ${"${CMAKE_CURRENT_SOURCE_DIR}"}/include/sdk-util_config.h)
find_path(IIO_INCLUDE_DIRS iio.h REQUIRED)
find_library(IIO_LIBRARIES NAMES iio libiio REQUIRED)

add_subdirectory(plugin/${plugin_dir})

qt_add_resources(PROJ_RES res/resources.qrc)

add_executable(${"${TARGET_NAME}"} ${"${PROJECT_SOURCES}"} ${"${PROJ_RES}"})

include(${"${CMAKE_CURRENT_SOURCE_DIR}"}/SdkSupport.cmake)

target_include_directories(${"${TARGET_NAME}"} PRIVATE ${"${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS}"})
target_include_directories(${"${TARGET_NAME}"} PRIVATE ${"${CMAKE_SOURCE_DIR}"}/include)
target_include_directories(${"${TARGET_NAME}"} INTERFACE ${"${IIO_INCLUDE_DIRS}"})

target_include_directories(${"${TARGET_NAME}"} PUBLIC ${"${SDK_DEPS_INCLUDE}"} ${"${IIO_INCLUDE_DIRS}"})

inlcude_dirs(${"${SDK_DEPS_INCLUDE}"})
# Add any extra libs to link also.
link_libs(${"${SDK_DEPS_LIB}"})
target_link_libraries(
${"${TARGET_NAME}"} PRIVATE Qt${"${QT_VERSION_MAJOR}"}::Widgets Qt${"${QT_VERSION_MAJOR}"}::Core ${"${IIO_LIBRARIES}"}
)
6 changes: 6 additions & 0 deletions tools/plugingenerator/templates/sdk/sdk_cmakein_template.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef SDK_UTIL_H_CMAKEIN
#define SDK_UTIL_H_CMAKEIN

#define PLUGIN_INSTALL_PATH "@PLUGIN_INSTALL_PATH@"

#endif // SDK_UTIL_H_CMAKEIN
Loading

0 comments on commit c7d7a69

Please sign in to comment.