diff --git a/CMakeLists.txt b/CMakeLists.txt index 3377afd..77b5644 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ option(LIEF_COVERAGE "Perform code coverage" OFF) option(LIEF_USE_CCACHE "Use ccache to speed up compilation" ON) option(LIEF_EXTRA_WARNINGS "Enable extra warning from the compiler" OFF) option(LIEF_LOGGING "Enable logging" ON) +option(LIEF_ENABLE_JSON "Enable JSON-related APIs" ON) option(LIEF_ELF "Build LIEF with ELF module" ON) option(LIEF_PE "Build LIEF with PE module" ON) @@ -74,17 +75,23 @@ endif() # Json # ---- -set(LIBJSON_GIT_URL "https://github.com/nlohmann/json.git" CACHE STRING "URL to the JSON lib repo") -ExternalProject_Add(lief_libjson - GIT_REPOSITORY ${LIBJSON_GIT_URL} - GIT_TAG v2.0.8 - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "") - -ExternalProject_get_property(lief_libjson SOURCE_DIR) -set(LIBJSON_SOURCE_DIR "${SOURCE_DIR}") +if (LIEF_ENABLE_JSON) + set(LIBJSON_GIT_URL "https://github.com/nlohmann/json.git" CACHE STRING "URL to the JSON lib repo") + ExternalProject_Add(lief_libjson + GIT_REPOSITORY ${LIBJSON_GIT_URL} + GIT_TAG v2.0.8 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "") + ExternalProject_get_property(lief_libjson SOURCE_DIR) + set(LIBJSON_SOURCE_DIR "${SOURCE_DIR}") + message(STATUS "Enable JSON support") + set(ENABLE_JSON_SUPPORT 1) +else() + message(STATUS "Disable JSON support") + set(ENABLE_JSON_SUPPORT 0) +endif() # Rang # ---- @@ -330,6 +337,12 @@ configure_file( "${CMAKE_CURRENT_BINARY_DIR}/include/LIEF/version.h" ) +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/include/LIEF/config.h.in" + "${CMAKE_CURRENT_BINARY_DIR}/include/LIEF/config.h" + @ONLY +) + set (LIEF_PUBLIC_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include/" "${CMAKE_CURRENT_SOURCE_DIR}/api/c/include/" @@ -375,8 +388,10 @@ target_include_directories(LIB_LIEF_SHARED PRIVATE "${LIEF_PRIVATE_INCLUDE_DIR}") -add_dependencies(LIB_LIEF_STATIC lief_libjson) -add_dependencies(LIB_LIEF_SHARED lief_libjson) +if (LIEF_ENABLE_JSON) + add_dependencies(LIB_LIEF_STATIC lief_libjson) + add_dependencies(LIB_LIEF_SHARED lief_libjson) +endif() add_dependencies(LIB_LIEF_STATIC lief_easyloggingpp) add_dependencies(LIB_LIEF_SHARED lief_easyloggingpp) diff --git a/api/python/CMakeLists.txt b/api/python/CMakeLists.txt index 88d4f69..a27d940 100644 --- a/api/python/CMakeLists.txt +++ b/api/python/CMakeLists.txt @@ -54,13 +54,20 @@ set(PYBIND11_SOURCE_DIR "${SOURCE_DIR}") set(LIEF_PYTHON_BASIC_SRC "${CMAKE_CURRENT_SOURCE_DIR}/pyLIEF.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/pyUtils.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/pyJson.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/pyIterators.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/pyExceptions.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/pyLogger.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/encoding.cpp" ) +if (LIEF_ENABLE_JSON) + set(LIEF_PYTHON_BASIC_SRC + $(LIEF_PYTHON_BASIC_SRC) + "${CMAKE_CURRENT_SOURCE_DIR}/pyJson.cpp" + ) +endif() + + set(LIEF_PYTHON_ABSTRACT_SRC "${CMAKE_CURRENT_SOURCE_DIR}/Abstract/init.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Abstract/objects" diff --git a/api/python/pyLIEF.cpp b/api/python/pyLIEF.cpp index 95bc808..5606d4b 100644 --- a/api/python/pyLIEF.cpp +++ b/api/python/pyLIEF.cpp @@ -51,7 +51,10 @@ PYBIND11_PLUGIN(_pylief) { // Init util functions init_utils_functions(LIEF_module); + +#if defined(LIEF_JSON_SUPPORT) init_json_functions(LIEF_module); +#endif return LIEF_module.ptr(); } diff --git a/api/python/pyLIEF.hpp b/api/python/pyLIEF.hpp index 476e0a6..1b56722 100644 --- a/api/python/pyLIEF.hpp +++ b/api/python/pyLIEF.hpp @@ -21,10 +21,14 @@ #include #include +#include + #include "encoding.hpp" #include "pyIterators.hpp" +#define RST_CLASS_REF(X) ":class:`~"#X"`" + namespace py = pybind11; using namespace pybind11::literals; @@ -36,8 +40,10 @@ void init_ELF_module(py::module&); void init_PE_module(py::module&); void init_MachO_module(py::module&); void init_utils_functions(py::module&); -void init_json_functions(py::module&); -#define RST_CLASS_REF(X) ":class:`~"#X"`" +#if defined(LIEF_JSON_SUPPORT) +void init_json_functions(py::module&); +#endif + #endif diff --git a/include/LIEF/config.h.in b/include/LIEF/config.h.in new file mode 100644 index 0000000..777489b --- /dev/null +++ b/include/LIEF/config.h.in @@ -0,0 +1,24 @@ +/* Copyright 2017 A. Guinet + * Copyright 2017 Quarkslab + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIEF_CONFIG_H_ +#define LIEF_CONFIG_H_ + +#if @ENABLE_JSON_SUPPORT@ +#define LIEF_JSON_SUPPORT +#endif + +#endif diff --git a/include/LIEF/json.hpp b/include/LIEF/json.hpp index db2adc8..bb0b95d 100644 --- a/include/LIEF/json.hpp +++ b/include/LIEF/json.hpp @@ -15,8 +15,13 @@ */ #ifndef LIEF_JSON_H_ #define LIEF_JSON_H_ + +#include + +#ifdef LIEF_JSON_SUPPORT #include using json = nlohmann::json; +#endif #endif diff --git a/include/LIEF/to_json.hpp b/include/LIEF/to_json.hpp index 0c321af..b5873c9 100644 --- a/include/LIEF/to_json.hpp +++ b/include/LIEF/to_json.hpp @@ -16,6 +16,10 @@ #ifndef LIEF_TO_JSON_H_ #define LIEF_TO_JSON_H_ +#include "LIEF/config.h" + +#ifdef LIEF_JSON_SUPPORT + #include "LIEF/json.hpp" #include "LIEF/visitors/json.hpp" @@ -41,4 +45,6 @@ std::string to_json_str(const T& obj) { } // namespace LIEF +#endif // LIEF_JSON_SUPPORT + #endif diff --git a/include/LIEF/visitors/elf_json.hpp b/include/LIEF/visitors/elf_json.hpp index 3b72abe..67f1fa0 100644 --- a/include/LIEF/visitors/elf_json.hpp +++ b/include/LIEF/visitors/elf_json.hpp @@ -16,6 +16,10 @@ #ifndef LIEF_ELF_VISITOR_JSONS_H_ #define LIEF_ELF_VISITOR_JSONS_H_ +#include "LIEF/config.h" + +#ifdef LIEF_JSON_SUPPORT + #include "LIEF/visibility.h" #include "LIEF/visitors/json.hpp" @@ -59,5 +63,6 @@ class DLL_PUBLIC JsonVisitor : public LIEF::JsonVisitor { } } +#endif // LIEF_JSON_SUPPORT #endif diff --git a/include/LIEF/visitors/json.hpp b/include/LIEF/visitors/json.hpp index 995966b..ae5dde6 100644 --- a/include/LIEF/visitors/json.hpp +++ b/include/LIEF/visitors/json.hpp @@ -16,6 +16,10 @@ #ifndef LIEF_VISITOR_JSONS_H_ #define LIEF_VISITOR_JSONS_H_ +#include "LIEF/config.h" + +#ifdef LIEF_JSON_SUPPORT + #include "LIEF/visibility.h" #include "LIEF/Visitor.hpp" #include "LIEF/json.hpp" @@ -46,5 +50,6 @@ class DLL_PUBLIC JsonVisitor : public Visitor { } +#endif // LIEF_JSON_SUPPORT #endif diff --git a/include/LIEF/visitors/pe_json.hpp b/include/LIEF/visitors/pe_json.hpp index ac46efb..31fc302 100644 --- a/include/LIEF/visitors/pe_json.hpp +++ b/include/LIEF/visitors/pe_json.hpp @@ -19,6 +19,10 @@ #include "LIEF/visibility.h" #include "LIEF/visitors/json.hpp" +#include "LIEF/config.h" + +#ifdef LIEF_JSON_SUPPORT + namespace LIEF { namespace PE { class DLL_PUBLIC JsonVisitor : public LIEF::JsonVisitor { @@ -72,5 +76,6 @@ class DLL_PUBLIC JsonVisitor : public LIEF::JsonVisitor { } } +#endif // LIEF_JSON_SUPPORT #endif diff --git a/src/visitors/elf_json.cpp b/src/visitors/elf_json.cpp index aa935df..03754d6 100644 --- a/src/visitors/elf_json.cpp +++ b/src/visitors/elf_json.cpp @@ -13,6 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#include "LIEF/config.h" + +#ifdef LIEF_JSON_SUPPORT + #include "LIEF/visitors/elf_json.hpp" #include "LIEF/ELF.hpp" namespace LIEF { @@ -411,3 +416,5 @@ void JsonVisitor::visit(const LIEF::Section& section) { } // namespace ELF } // namespace LIEF + +#endif // LIEF_JSON_SUPPORT diff --git a/src/visitors/json.cpp b/src/visitors/json.cpp index 4cd2bdf..29ef637 100644 --- a/src/visitors/json.cpp +++ b/src/visitors/json.cpp @@ -16,6 +16,11 @@ #include "LIEF/Abstract/Abstract.hpp" #include "LIEF/visitors/json.hpp" #include "LIEF/Abstract/EnumToString.hpp" + +#include "LIEF/config.h" + +#ifdef LIEF_JSON_SUPPORT + namespace LIEF { JsonVisitor::JsonVisitor(void) : @@ -92,3 +97,5 @@ const json& JsonVisitor::get(void) const { } } + +#endif // LIEF_JSON_SUPPORT diff --git a/src/visitors/pe_json.cpp b/src/visitors/pe_json.cpp index 514395e..08088d1 100644 --- a/src/visitors/pe_json.cpp +++ b/src/visitors/pe_json.cpp @@ -13,6 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#include "LIEF/config.h" + +#ifdef LIEF_JSON_SUPPORT + #include "LIEF/visitors/pe_json.hpp" #include "LIEF/visitors/Hash.hpp" #include "LIEF/PE.hpp" @@ -661,3 +666,5 @@ void JsonVisitor::visit(const LIEF::Section& section) { } // namespace PE } // namespace LIEF + +#endif // LIEF_JSON_SUPPORT