Skip to content

Commit

Permalink
Merge pull request #255 from Meakk/options-rework
Browse files Browse the repository at this point in the history
Use external json lib and move files to app
  • Loading branch information
mwestphal authored May 1, 2022
2 parents f461044 + 923402c commit 71565a6
Show file tree
Hide file tree
Showing 26 changed files with 22,263 additions and 94 deletions.
3 changes: 1 addition & 2 deletions .github/actions/vtk-install-dep/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ runs:
uses: actions/cache@v2
with:
path: dependencies/vtk_install
key: vtk-${{env.VTK_SHA_OR_TAG}}-4-${{inputs.os}}
key: vtk-${{env.VTK_SHA_OR_TAG}}-5-${{inputs.os}}

- name: Setup VTK
if: steps.cache-vtk.outputs.cache-hit != 'true'
Expand Down Expand Up @@ -106,7 +106,6 @@ runs:
-DVTK_MODULE_ENABLE_VTK_RenderingOpenGL2=YES
-DVTK_MODULE_ENABLE_VTK_RenderingVolumeOpenGL2=YES
-DVTK_MODULE_ENABLE_VTK_TestingCore=YES
-DVTK_MODULE_ENABLE_VTK_jsoncpp=YES
${{ inputs.vtk_version != 'v9.0.0' && '-DVTK_ENABLE_REMOTE_MODULES=OFF -DVTK_SMP_IMPLEMENTATION_TYPE=STDThread' || null }}
${{ inputs.os == 'windows-latest' && '-Ax64 -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded' || null }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ jobs:
lcov --base-directory . --directory . -c -o coverage.info
lcov --remove coverage.info "*/dependencies/*" -o coverage.info
lcov --remove coverage.info "*/cxxopts.hpp" -o coverage.info
lcov --remove coverage.info "*/json.hpp" -o coverage.info
lcov --remove coverage.info "*Test*" -o coverage.info
- name: Upload coverage to Codecov
Expand Down
2 changes: 2 additions & 0 deletions cmake/f3dConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

include("${CMAKE_CURRENT_LIST_DIR}/f3dTargets.cmake")

include("${CMAKE_CURRENT_LIST_DIR}/f3dEmbed.cmake")

set_and_check(f3d_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")

check_required_components(f3d)
56 changes: 56 additions & 0 deletions cmake/f3dEmbed.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Modified from vtkEncodeString (BSD licensed)

set(_embed_script_file "${CMAKE_CURRENT_LIST_FILE}")

function (f3d_embed_file)
cmake_parse_arguments(PARSE_ARGV 0 _embed
""
"INPUT;NAME"
"")

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_embed_NAME}.h"
"${CMAKE_CURRENT_BINARY_DIR}/${_embed_NAME}.cxx"
DEPENDS "${_embed_script_file}"
"${_embed_INPUT}"
COMMAND "${CMAKE_COMMAND}"
"-Dbinary_dir=${CMAKE_CURRENT_BINARY_DIR}"
"-Dsource_file=${_embed_INPUT}"
"-Doutput_name=${_embed_NAME}"
"-D_embed_run=ON"
-P "${_embed_script_file}")
endfunction ()

if (_embed_run AND CMAKE_SCRIPT_MODE_FILE)
set(output_header "${binary_dir}/${output_name}.h")
set(output_source "${binary_dir}/${output_name}.cxx")

file(WRITE "${output_header}" "")
file(WRITE "${output_source}" "")

file(APPEND "${output_header}"
"#ifndef ${output_name}_h\n\n#define ${output_name}_h\n\n")

# Read the file in HEX format
file(READ "${source_file}" original_content HEX)

string(LENGTH "${original_content}" output_size)
math(EXPR output_size "${output_size} / 2")

file(APPEND "${output_header}"
"extern const unsigned char ${output_name}[${output_size}];\n\n#endif\n")

file(APPEND "${output_source}"
"#include \"${output_name}.h\"\n\nconst unsigned char ${output_name}[${output_size}] = {\n")
string(REGEX REPLACE "\([0-9a-f][0-9a-f]\)" ",0x\\1" escaped_content "${original_content}")
# Hard line wrap the file.
string(REGEX REPLACE "\(...............................................................................,\)" "\\1\n" escaped_content "${escaped_content}")

# Remove the leading comma.
string(REGEX REPLACE "^," "" escaped_content "${escaped_content}")

file(APPEND "${output_source}"
"${escaped_content}\n")
file(APPEND "${output_source}"
"};\n")
endif ()
9 changes: 7 additions & 2 deletions cmake/installing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ if(F3D_INSTALL_SDK)
"${CMAKE_CURRENT_BINARY_DIR}/f3dConfigVersion.cmake"
VERSION "${PROJECT_VERSION}.${f3d_VERSION_BUILD}"
COMPATIBILITY SameMinorVersion)

install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/f3dConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/f3dConfigVersion.cmake"
DESTINATION "lib/cmake/${PROJECT_NAME}")
FILES
"${CMAKE_CURRENT_BINARY_DIR}/f3dConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/f3dConfigVersion.cmake"
"${CMAKE_CURRENT_LIST_DIR}/f3dEmbed.cmake"
DESTINATION
"lib/cmake/${PROJECT_NAME}")
endif()
2 changes: 1 addition & 1 deletion resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"ssao": true,
"scalars": "",
"comp": "-2",
"cells": "true"
"cells": true
},
".*(tif|tiff)":
{
Expand Down
31 changes: 27 additions & 4 deletions src/application/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
set(EXEC_PLATFORM_SPECIFIC_FILES "")
# Generate F3DIcon buffer
include("${CMAKE_SOURCE_DIR}/cmake/f3dEmbed.cmake")

f3d_embed_file(
INPUT "${CMAKE_SOURCE_DIR}/resources/logo32.png"
NAME F3DIcon
BINARY)

set(F3D_SOURCE_FILES
${CMAKE_CURRENT_BINARY_DIR}/F3DIcon.cxx
${CMAKE_CURRENT_SOURCE_DIR}/main.cxx
${CMAKE_CURRENT_SOURCE_DIR}/F3DStarter.cxx
)

if(WIN32)
set(EXEC_PLATFORM_SPECIFIC_FILES "${CMAKE_SOURCE_DIR}/resources/f3d.rc")
list(APPEND F3D_SOURCE_FILES "${CMAKE_SOURCE_DIR}/resources/f3d.rc")
endif()

if(APPLE)
list(APPEND F3D_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/F3DNSDelegate.mm")
endif()

# Define f3d target
add_executable(f3d main.cxx ${EXEC_PLATFORM_SPECIFIC_FILES})
add_executable(f3d ${F3D_SOURCE_FILES})
target_link_libraries(f3d PUBLIC libf3d)
set_target_properties(f3d PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
if (APPLE)
Expand All @@ -13,7 +30,13 @@ elseif (UNIX)
set_target_properties(f3d PROPERTIES INSTALL_RPATH $ORIGIN/../lib)
endif ()

target_compile_features(f3d PRIVATE cxx_std_11)
# Cannot upgrade to C++17 here, because <codecvt> header is deprecated and produces warnings on Windows
target_compile_features(f3d PRIVATE cxx_std_14)

target_include_directories(f3d
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
)

if(F3D_STRICT_BUILD)
if(MSVC)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int F3DStarter::Start(int argc, char** argv)
this->LoadFile(f3d::loader::LoadFileEnum::LOAD_LAST);
return true;
});
this->Internals->Engine->getWindow().setWindowName(f3d::AppTitle);
this->Internals->Engine->getWindow().setWindowName(f3d::engine::getAppTitle());
this->Internals->Engine->getWindow().setIcon(F3DIcon, sizeof(F3DIcon));
}

Expand Down
11 changes: 3 additions & 8 deletions src/library/F3DStarter.h → src/application/F3DStarter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
#ifndef F3DStarter_h
#define F3DStarter_h

// TODO: this file is used in the application so it needs to be exported.
// However, it will be moved completely in the application at some point, then export of used APIs
// can be removed
#include "f3d_export.h"

#include "f3d_loader.h"

class F3DStarter
Expand All @@ -20,7 +15,7 @@ class F3DStarter
/**
* Parse the options and configure a f3d::loader accordingly
*/
F3D_EXPORT int Start(int argc, char** argv);
int Start(int argc, char** argv);

/**
* Add a file or directory to be forwarded to the loader
Expand All @@ -34,8 +29,8 @@ class F3DStarter
*/
bool LoadFile(f3d::loader::LoadFileEnum load = f3d::loader::LoadFileEnum::LOAD_CURRENT);

F3D_EXPORT F3DStarter();
F3D_EXPORT ~F3DStarter();
F3DStarter();
~F3DStarter();

private:
class F3DInternals;
Expand Down
1 change: 0 additions & 1 deletion src/application/main.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "F3DException.h"
#include "F3DStarter.h"
#include "f3d_config.h"
#include "f3d_log.h"

int main(int argc, char** argv)
Expand Down
12 changes: 0 additions & 12 deletions src/library/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# Generate F3DIcon buffer
vtk_encode_string(
INPUT "${CMAKE_SOURCE_DIR}/resources/logo32.png"
NAME F3DIcon
BINARY)

# Define VTK modules to link libf3d with
# Note that readers/importers specific modules
# should be listed here as reader instanciation is header only
Expand Down Expand Up @@ -130,9 +124,7 @@ configure_file(
# Define libf3d target
set(F3D_SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/F3DAnimationManager.cxx
${CMAKE_CURRENT_BINARY_DIR}/F3DIcon.cxx
${CMAKE_CURRENT_SOURCE_DIR}/F3DOptions.cxx
${CMAKE_CURRENT_SOURCE_DIR}/F3DStarter.cxx
${CMAKE_CURRENT_BINARY_DIR}/f3d_config.cxx
${CMAKE_CURRENT_SOURCE_DIR}/f3d_init.cxx
${CMAKE_CURRENT_SOURCE_DIR}/f3d_engine.cxx
Expand All @@ -145,10 +137,6 @@ set(F3D_SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/f3d_window_impl_noRender.cxx
)

if(APPLE)
list(APPEND F3D_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/F3DNSDelegate.mm")
endif()

# List of headers that will be installed
set(F3D_PUBLIC_HEADERS
${CMAKE_CURRENT_BINARY_DIR}/f3d_export.h
Expand Down
5 changes: 0 additions & 5 deletions src/library/F3DAnimationManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
#include "f3d_log.h"
#include "f3d_options.h"
#include "f3d_window.h"
#include "vtkF3DRenderer.h"

#include <vtkCallbackCommand.h>
#include <vtkDoubleArray.h>
#include <vtkImporter.h>
#include <vtkProgressBarRepresentation.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRendererCollection.h>
#include <vtkVersion.h>

#include <functional>
Expand Down
46 changes: 31 additions & 15 deletions src/library/F3DOptions.cxx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "F3DOptions.h"

#include "cxxopts.hpp"
#include "json.hpp"

#include "F3DException.h"
#include "f3d_config.h"
#include "f3d_engine.h"
#include "f3d_log.h"
#include "f3d_options.h"

#include <vtk_jsoncpp.h>
#include <vtksys/SystemTools.hxx>

#include <fstream>
Expand All @@ -18,6 +18,12 @@
#include <utility>
#include <vector>

//----------------------------------------------------------------------------
F3DOptions::F3DOptions()
{
this->Scalars = f3d::ReservedString; // XXX this should be removable at some point
}

//----------------------------------------------------------------------------
class ConfigurationOptions
{
Expand Down Expand Up @@ -416,29 +422,39 @@ bool ConfigurationOptions::InitializeDictionaryFromConfigFile(const std::string&
return false;
}

Json::Value root;
Json::CharReaderBuilder builder;
builder["collectComments"] = false;
std::string errs;
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
bool success = Json::parseFromStream(builder, file, &root, &errs);
if (!success)
nlohmann::json j;

try
{
file >> j;
}
catch (const std::exception& ex)
{
f3d::log::error("Unable to parse the configuration file ", configFilePath);
f3d::log::error(errs);
f3d::log::error(ex.what());
return false;
}

for (auto const& id : root.getMemberNames())
for (const auto& regexpConfig : j.items())
{
const Json::Value node = root[id];
std::map<std::string, std::string> localDic;
for (auto const& nl : node.getMemberNames())
for (const auto& prop : regexpConfig.value().items())
{
const Json::Value v = node[nl];
localDic[nl] = v.asString();
if (prop.value().is_number() || prop.value().is_boolean())
{
localDic[prop.key()] = ToString(prop.value());
}
else if (prop.value().is_string())
{
localDic[prop.key()] = prop.value().get<std::string>();
}
else
{
f3d::log::error(prop.key(), " must be a string, a boolean or a number");
return false;
}
}
this->ConfigDic[id] = localDic;
this->ConfigDic[regexpConfig.key()] = localDic;
}

return true;
Expand Down
Loading

0 comments on commit 71565a6

Please sign in to comment.