Skip to content

Commit

Permalink
Add CMake option for Windows API detection
Browse files Browse the repository at this point in the history
Introduce `libCZI_WINDOWSAPIAVAILABLE` to detect Windows-based build environments (WIN32, CYGWIN, MSYS, MINGW). Update `CMakeLists.txt` to set this variable and add messages indicating Windows API availability. Configure `libCZI_Config.h` to include `LIBCZI_WINDOWSAPI_AVAILABLE` macro.

Replace `_WIN32` with `LIBCZI_WINDOWSAPI_AVAILABLE` for conditional compilation in multiple files: `StreamImpl.cpp`, `StreamImpl.h`, `windowsfileinputstream.cpp`, `windowsfileinputstream.h`, `decoder_wic.cpp`, `decoder_wic.h`, `libCZI.h`, `libCZI_Site.cpp`, and `utilities.cpp`.

Ensure Windows-specific code is conditionally compiled based on `LIBCZI_WINDOWSAPI_AVAILABLE`, enhancing portability and maintainability.
  • Loading branch information
ptahmose committed Sep 19, 2024
1 parent 333365d commit d3cba5d
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 27 deletions.
15 changes: 14 additions & 1 deletion Src/libCZI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ else()
endif()
endif()

if (WIN32 OR CYGWIN OR MSYS OR MINGW)
set(libCZI_WINDOWSAPIAVAILABLE 1)
message(STATUS "*********** Windows-API available WIN32 is true ***********")
else()
set(libCZI_WINDOWSAPIAVAILABLE 0)
message(STATUS "*********** Windows-API NOT available WIN32 is false ***********")
endif()

configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/libCZI_Config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/libCZI_Config.h"
Expand Down Expand Up @@ -304,6 +312,9 @@ if (LIBCZI_BUILD_DYNLIB)
IF(UNIX)
target_compile_options(libCZI PRIVATE -D_FILE_OFFSET_BITS=64)
ENDIF(UNIX)
if( libCZI_WINDOWSAPIAVAILABLE)
target_link_libraries(libCZI PRIVATE ole32 Windowscodecs)
endif()
endif(LIBCZI_BUILD_DYNLIB)

#
Expand Down Expand Up @@ -342,7 +353,9 @@ IF(UNIX)
target_compile_options(libCZIStatic PRIVATE -D_FILE_OFFSET_BITS=64)
set_property(TARGET libCZIStatic PROPERTY POSITION_INDEPENDENT_CODE ON)
ENDIF(UNIX)

if( libCZI_WINDOWSAPIAVAILABLE)
target_link_libraries(libCZIStatic PUBLIC ole32 Windowscodecs)
endif()

#
# Notes: - the variables CMAKE_INSTALL_<...> have been defined in the module "GNUInstallDirs"
Expand Down
4 changes: 2 additions & 2 deletions Src/libCZI/StreamImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ COutputStreamImplPwrite::~COutputStreamImplPwrite()

//----------------------------------------------------------------------------

#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE

CSimpleOutputStreamImplWindows::CSimpleOutputStreamImplWindows(const wchar_t* filename, bool overwriteExisting)
: handle(INVALID_HANDLE_VALUE)
Expand Down Expand Up @@ -316,7 +316,7 @@ CInputOutputStreamImplPreadPwrite::~CInputOutputStreamImplPreadPwrite()

//-----------------------------------------------------------------------------

#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
CSimpleInputOutputStreamImplWindows::CSimpleInputOutputStreamImplWindows(const wchar_t* filename)
: handle(INVALID_HANDLE_VALUE)
{
Expand Down
10 changes: 5 additions & 5 deletions Src/libCZI/StreamImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

#pragma once

#include <memory>
#include "libCZI.h"
#include "inc_libCZI_Config.h"
#if defined(_WIN32)
#include <Windows.h>
#include <memory>
#if LIBCZI_WINDOWSAPI_AVAILABLE
#include <Windows.h>
#endif

/// <summary> A simplistic output-stream implementation (based on C-runtime fopen). Note that this implementation is NOT thread-safe.</summary>
Expand Down Expand Up @@ -40,7 +40,7 @@ class COutputStreamImplPwrite : public libCZI::IOutputStream
};
#endif

#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE

class CSimpleOutputStreamImplWindows : public libCZI::IOutputStream
{
Expand Down Expand Up @@ -84,7 +84,7 @@ class CInputOutputStreamImplPreadPwrite : public libCZI::IInputOutputStream
};
#endif

#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
class CSimpleInputOutputStreamImplWindows : public libCZI::IInputOutputStream
{
private:
Expand Down
2 changes: 1 addition & 1 deletion Src/libCZI/StreamsLib/windowsfileinputstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "windowsfileinputstream.h"

#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
#include <limits>
#include <iomanip>
#include "../utilities.h"
Expand Down
2 changes: 1 addition & 1 deletion Src/libCZI/StreamsLib/windowsfileinputstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma once
#include <libCZI_Config.h>

#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
#include <string>
#include "../libCZI.h"
#include <Windows.h>
Expand Down
7 changes: 4 additions & 3 deletions Src/libCZI/decoder_wic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#if defined(_WIN32)
#include "decoder_wic.h"

#if LIBCZI_WINDOWSAPI_AVAILABLE
#include "BitmapOperations.h"
#include <wincodec.h>

#include "Site.h"

#pragma comment(lib, "Windowscodecs.lib")
//
//#pragma comment(lib, "Windowscodecs.lib")

using namespace std;
using namespace libCZI;
Expand Down
4 changes: 3 additions & 1 deletion Src/libCZI/decoder_wic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#pragma once

#if defined(_WIN32)
#include <libCZI_Config.h>

#if LIBCZI_WINDOWSAPI_AVAILABLE

#include <memory>
#include "libCZI_Pixels.h"
Expand Down
6 changes: 2 additions & 4 deletions Src/libCZI/libCZI.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ namespace libCZI
enum class SiteObjectType
{
Default, ///< An enum constant representing the default option (which is JXRLib)
WithJxrDecoder ///< An enum constant representing a Site-object using the internal JXRLib.
#if defined(_WIN32)
, WithWICDecoder///< An enum constant representing a Site-object using the Windows WIC-codec.
#endif
WithJxrDecoder, ///< An enum constant representing a Site-object using the internal JXRLib.
WithWICDecoder, ///< An enum constant representing a Site-object using the Windows WIC-codec.
};

class ISite;
Expand Down
3 changes: 3 additions & 0 deletions Src/libCZI/libCZI_Config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#define LIBCZI_VERSION_PATCH "@libCZI_VERSION_PATCH@"
#define LIBCZI_VERSION_TWEAK "@libCZI_VERSION_TWEAK@"

// whether the libCZI library is built with support for the Windows-API, i.e. whether it is being built for a Windows-environment
#define LIBCZI_WINDOWSAPI_AVAILABLE @libCZI_WINDOWSAPIAVAILABLE@

// if the host system is a big-endian system, this is "1", otherwise 0
#define LIBCZI_ISBIGENDIANHOST @libCZI_ISBIGENDIANHOST@

Expand Down
7 changes: 4 additions & 3 deletions Src/libCZI/libCZI_Site.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "libCZI.h"
#include "inc_libCZI_Config.h"
#include "decoder.h"
#include "decoder_zstd.h"
#include <mutex>
Expand Down Expand Up @@ -37,7 +38,7 @@ class CSiteImpBase : public ISite
}
};

#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
class CSiteImpWic : public CSiteImpBase
{
private:
Expand Down Expand Up @@ -139,7 +140,7 @@ class CSiteImpJxrLib : public CSiteImpBase
}
};

#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
static CSiteImpWic theWicSite;
#endif
static CSiteImpJxrLib theJxrLibSite;
Expand Down Expand Up @@ -168,7 +169,7 @@ libCZI::ISite* libCZI::GetDefaultSiteObject(SiteObjectType type)
{
switch (type)
{
#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
case SiteObjectType::WithWICDecoder:
return &theWicSite;
#endif
Expand Down
11 changes: 5 additions & 6 deletions Src/libCZI/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "utilities.h"
#include "inc_libCZI_Config.h"
#include <locale>
#include <codecvt>
#include <sstream>
#include <cstring>
#include <array>
#if !defined(_WIN32)
#include <random>
#endif
#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
#include <Windows.h>
#else
#include <random>
#endif
#include "inc_libCZI_Config.h"
#if LIBCZI_HAVE_ENDIAN_H
#include "endian.h"
#endif
Expand Down Expand Up @@ -124,7 +123,7 @@ tString trimImpl(const tString& str, const tString& whitespace)

/*static*/libCZI::GUID Utilities::GenerateNewGuid()
{
#if defined(_WIN32)
#if LIBCZI_WINDOWSAPI_AVAILABLE
::GUID guid;
CoCreateGuid(&guid);
libCZI::GUID guid_value
Expand Down

0 comments on commit d3cba5d

Please sign in to comment.