Commit 6056ea62 authored by jan.koester's avatar jan.koester
Browse files

windows fixes

parent 7a72a1b8
Loading
Loading
Loading
Loading
Loading
+85 −31
Changes for cmake/Findjson-c.cmake: 85 added lines, 31 removed lines.
Original line number Diff line number Diff line
# @project      The CERN Tape Archive (CTA)
# @copyright    Copyright © 2015-2022 CERN
# @license      This program is free software, distributed under the terms of the GNU General Public
#               Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". You can
#               redistribute it and/or modify it under the terms of the GPL Version 3, or (at your
#               option) any later version.
#
#               This program is distributed in the hope that it will be useful, but WITHOUT ANY
#               WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
#               PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
#               In applying this licence, CERN does not waive the privileges and immunities
#               granted to it by virtue of its status as an Intergovernmental Organization or
#               submit itself to any jurisdiction.

# This module will set the following variables:
#     JSON-C_FOUND
#     JSON-C_INCLUDE_DIRS
#     JSON-C_LIBRARIES

find_path(JSON-C_INCLUDE_DIRS
  json.h
  PATHS /usr/include/json-c
  NO_DEFAULT_PATH)

find_library(JSON-C_LIBRARIES
  NAME json-c
  PATHS /usr/lib64
  NO_DEFAULT_PATH)
# =====================================================================
# Findjson-c.cmake (Shared Library Version - Refined for Windows DLL Paths)
# =====================================================================

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(json-c DEFAULT_MSG
  JSON-C_INCLUDE_DIRS JSON-C_LIBRARIES)

# -----------------------------------------------------------------------------
# 1. Find Header and Link Libraries
# -----------------------------------------------------------------------------

# Find the header file (e.g., json-c/json.h or json.h)
find_path(JSONC_INCLUDE_DIR NAMES json-c/json.h json.h
    HINTS ENV JSONC_ROOT
    PATH_SUFFIXES include
)

# Find the shared library file (.lib on Windows, .so/.dylib elsewhere)
# json-c library names can vary (e.g., json-c, json-c-0, json-c.so.0)
find_library(JSONC_LIBRARY NAMES json-c json-c-0)

# -----------------------------------------------------------------------------
# 2. Handle Required Arguments
# -----------------------------------------------------------------------------

find_package_handle_standard_args(JsonC
    FOUND_VAR           JSONC_FOUND
    REQUIRED_VARS       JSONC_INCLUDE_DIR
                        JSONC_LIBRARY
    FAIL_MESSAGE        "Could NOT find json-c shared library or headers."
)

# -----------------------------------------------------------------------------
# 3. Define Imported Shared Target
# -----------------------------------------------------------------------------

if(JSONC_FOUND)
    
    # 1. Define the target as SHARED IMPORTED
    # We use a single target since json-c is generally one library.
    add_library(JsonC::JsonC SHARED IMPORTED)
    
    # 2. Set the public interface (headers)
    set_target_properties(JsonC::JsonC PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${JSONC_INCLUDE_DIR}"
    )
    
    # 3. Set the location properties
    if(WIN32)
        # Set IMPORTED_IMPLIB (the .lib) for linking
        set_target_properties(JsonC::JsonC PROPERTIES
            IMPORTED_IMPLIB "${JSONC_LIBRARY}"
        )
        
        # CRITICAL FOR DLL PATH RESOLUTION: Determine the DLL path.
        # We assume a common structure: DLLs are in 'bin' and .lib are in 'lib'.
        get_filename_component(_LIB_DIR "${JSONC_LIBRARY}" DIRECTORY)
        get_filename_component(_PARENT_DIR "${_LIB_DIR}" DIRECTORY)
        
        # Determine the DLL name (strip prefix/suffix, often just 'json-c')
        get_filename_component(_LIB_NAME_WE "${JSONC_LIBRARY}" NAME_WE)
        string(REGEX REPLACE "^lib" "" _DLL_NAME_BASE "${_LIB_NAME_WE}")
        
        # 💡 Heuristic: Assuming DLL is in <parent>/bin/
        # Set IMPORTED_LOCATION for both Debug and Release configurations
        set_target_properties(JsonC::JsonC PROPERTIES
            # Assuming VCPKG/standard installs put the DLL in bin/
            IMPORTED_LOCATION_RELEASE "${_PARENT_DIR}/bin/${_DLL_NAME_BASE}.dll"
            # Assuming a common debug suffix 'd' (e.g., json-cd.dll)
            IMPORTED_LOCATION_DEBUG   "${_PARENT_DIR}/bin/${_DLL_NAME_BASE}d.dll"
            # Add RelWithDebInfo as well, which might have the same DLL name as Release
            IMPORTED_LOCATION_RELWITHDEBINFO "${_PARENT_DIR}/bin/${_DLL_NAME_BASE}.dll"
        )
        
    else()
        # Non-Windows systems: link file is the runtime file
        set_target_properties(JsonC::JsonC PROPERTIES
            IMPORTED_LOCATION "${JSONC_LIBRARY}"
        )
    endif()
    
endif()

# Hide internal variables from the CMake GUI
mark_as_advanced(
    JSONC_INCLUDE_DIR
    JSONC_LIBRARY
)
 No newline at end of file