Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions Modules/ThirdParty/MetaIO/src/MetaIO/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@

if(METAIO_FOR_VTK)
set(METAIO_NAMESPACE "vtkmetaio")
set(METAIO_TARGET "metaio")
set(METAIO_TARGET "vtkmetaio")
set(METAIO_INSTALL_NO_LIBRARIES TRUE)
set(METAIO_INSTALL_NO_DEVELOPMENT TRUE)
if(BUILD_SHARED_LIBS)
add_definitions(-Dvtkmetaio_BUILD_SHARED_LIBS)
endif()
elseif("${METAIO_NAMESPACE}" STREQUAL "ITKMetaIO")
set(METAIO_FOR_ITK 1)
elseif(NOT METAIO_NAMESPACE)
if(BUILD_SHARED_LIBS)
add_definitions(-Ditkmetaio_BUILD_SHARED_LIBS)
endif()
elseif("${METAIO_NAMESPACE}" STREQUAL "MetaIO" OR NOT METAIO_NAMESPACE)
set(METAIO_NAMESPACE MetaIO)
set(METAIO_FOR_METAIO 1)
if(BUILD_SHARED_LIBS)
add_definitions(-Dmetaio_BUILD_SHARED_LIBS)
endif()
endif()

if(NOT DEFINED METAIO_TARGET)
Expand Down Expand Up @@ -117,21 +124,23 @@ if (METAIO_FOR_VTK)
SOURCES ${sources}
HEADERS ${headers}
HEADERS_SUBDIR "vtkmetaio")
vtk_module_set_property(VTK::metaio
PROPERTY DEFINE_SYMBOL
VALUE metaio_EXPORTS)
else ()
add_library(${METAIO_TARGET}
${sources}
${headers}
)
if (METAIO_FOR_ITK)
target_link_libraries(${METAIO_TARGET} PUBLIC
itksys
)
endif (METAIO_FOR_ITK)
endif ()

include_regular_expression("^.*$")

# Need nsl to resolve gethostbyname on SunOS-5.8
# and socket also
if(CMAKE_SYSTEM MATCHES "SunOS.*")
target_link_libraries(${METAIO_TARGET} PRIVATE socket nsl)
endif()

target_link_libraries(${METAIO_TARGET} PUBLIC
${METAIO_LIBXML2_LIBRARIES}
${METAIO_ZLIB_LIBRARIES}
Expand Down
63 changes: 56 additions & 7 deletions Modules/ThirdParty/MetaIO/src/MetaIO/src/localMetaConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,44 @@

#include "metaIOConfig.h"

#if defined(METAIO_FOR_ITK) || !defined(METAIO_FOR_VTK)
#if defined(METAIO_FOR_ITK)
// ITK

# define METAIO_USE_NAMESPACE 0
# define METAIO_NAMESPACE ITKMetaIO
# define METAIO_STREAM itksys

# include "itk_zlib.h"
# include <itksys/FStream.hxx>
# include <itk_zlib.h>

# include <iostream>
# include <fstream>

# define METAIO_EXPORT
# if defined(_WIN32) && defined(itkmetaio_BUILD_SHARED_LIBS)
# ifdef metaio_EXPORTS
# define METAIO_EXPORT __declspec(dllexport)
# define METAIO_EXTERN
# else
# define METAIO_EXPORT __declspec(dllimport)
Comment on lines +38 to +42

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Export ITKMetaIO symbols

In Windows shared builds, ITKMetaIO receives the target definition ITKMetaIO_EXPORTS, but this branch checks metaio_EXPORTS. The condition is therefore false while compiling the library itself, causing its public declarations to use __declspec(dllimport) instead of __declspec(dllexport). This prevents the shared library from exporting its API correctly; test ITKMetaIO_EXPORTS here or configure the target export definition consistently.

Knowledge Base Used: Module build and distribution system

Artifacts

Windows export macro reproduction

  • The script preprocesses the header with the ITK shared-library target definitions and compares the current and corrected export macro expansions.

Current export macro expansion

  • The current header expands the public API macro to a DLL import declaration while compiling the library.

Corrected export macro expansion

  • Changing the tested definition to the ITKMetaIO target definition expands the public API macro to a DLL export declaration.

ITKMetaIO target definition inspection

  • The inspected CMake configuration shows that the ITK build target is named ITKMetaIO, establishing the expected target export definition.

View artifacts

T-Rex Ran code and verified through T-Rex

# define METAIO_EXTERN extern
# endif
# else
# if defined(itkmetaio_BUILD_SHARED_LIBS)
# define METAIO_EXPORT __attribute__((visibility ("default")))
# else
# define METAIO_EXPORT
# endif
# endif

#else
#elif defined(METAIO_FOR_VTK)
// VTK

# define METAIO_USE_NAMESPACE 1
# define METAIO_NAMESPACE vtkmetaio
# define METAIO_STREAM vtksys

# include "vtk_zlib.h"
# include <vtksys/FStream.hxx>
# include <vtk_zlib.h>

# include <iostream>
# include <fstream>
Expand All @@ -54,8 +72,39 @@
# define METAIO_EXTERN extern
# endif
# else
# define METAIO_EXPORT
# if defined(vtkmetaio_BUILD_SHARED_LIBS)
# define METAIO_EXPORT __attribute__((visibility ("default")))
# else
# define METAIO_EXPORT
# endif
# endif

#else
// Independent of ITK and VTK

# define METAIO_USE_NAMESPACE 0
# define METAIO_NAMESPACE metaio
# define METAIO_STREAM std

# include "itk_zlib.h"

# include <iostream>
# include <fstream>

# if defined(_WIN32) && defined(metaio_BUILD_SHARED_LIBS)
# ifdef metaio_EXPORTS
# define METAIO_EXPORT __declspec(dllexport)
# define METAIO_EXTERN
# else
# define METAIO_EXPORT __declspec(dllimport)
# define METAIO_EXTERN extern
# endif
# else
# if defined(metaio_BUILD_SHARED_LIBS)
# define METAIO_EXPORT __attribute__((visibility ("default")))
# else
# define METAIO_EXPORT
# endif
# endif

// end VTK/ITK
#endif
80 changes: 28 additions & 52 deletions Modules/ThirdParty/MetaIO/src/MetaIO/src/metaArray.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,9 @@ MetaArray::CanRead(const char * _headerName) const
}

// Now check the file content
std::ifstream inputStream;
METAIO_STREAM::ifstream inputStream;

#ifdef __sgi
inputStream.open(_headerName, std::ios::in);
#else
inputStream.open(_headerName, std::ios::in | std::ios::binary);
#endif

if (!inputStream.rdbuf()->is_open())
{
Expand All @@ -581,13 +577,9 @@ MetaArray::Read(const char * _headerName, bool _readElements, void * _elementDat
m_FileName = _headerName;
}

auto * tmpStream = new std::ifstream;
auto * tmpStream = new METAIO_STREAM::ifstream;

#ifdef __sgi
tmpStream->open(m_FileName, std::ios::in);
#else
tmpStream->open(m_FileName, std::ios::in | std::ios::binary);
#endif
tmpStream->open(m_FileName.c_str(), std::ios::in | std::ios::binary);

if (!tmpStream->rdbuf()->is_open())
{
Expand All @@ -612,7 +604,7 @@ MetaArray::Read(const char * _headerName, bool _readElements, void * _elementDat


bool
MetaArray::CanReadStream(std::ifstream * _stream) const
MetaArray::CanReadStream(METAIO_STREAM::ifstream * _stream) const
{
if (!strncmp(MET_ReadForm(*_stream).c_str(), "Array", 5))
{
Expand All @@ -622,7 +614,7 @@ MetaArray::CanReadStream(std::ifstream * _stream) const
}

bool
MetaArray::ReadStream(std::ifstream * _stream, bool _readElements, void * _elementDataBuffer, bool _autoFreeElementData)
MetaArray::ReadStream(METAIO_STREAM::ifstream * _stream, bool _readElements, void * _elementDataBuffer, bool _autoFreeElementData)
{
META_DEBUG_PRINT( "MetaArray: ReadStream" );

Expand Down Expand Up @@ -671,13 +663,9 @@ MetaArray::ReadStream(std::ifstream * _stream, bool _readElements, void * _eleme
{
fName = m_ElementDataFileName;
}
auto * readStreamTemp = new std::ifstream;
auto * readStreamTemp = new METAIO_STREAM::ifstream;

#ifdef __sgi
readStreamTemp->open(fName, std::ios::in);
#else
readStreamTemp->open(fName, std::ios::binary | std::ios::in);
#endif
readStreamTemp->open(fName.c_str(), std::ios::binary | std::ios::in);
if (!readStreamTemp->rdbuf()->is_open())
{
std::cout << "MetaArray: Read: Cannot open data file" << '\n';
Expand Down Expand Up @@ -756,19 +744,9 @@ MetaArray::Write(const char * _headName, const char * _dataName, bool _writeElem
}
}

auto * tmpWriteStream = new std::ofstream;
auto * tmpWriteStream = new METAIO_STREAM::ofstream;

// Some older sgi compilers have a error in the ofstream constructor
// that requires a file to exist for output
#ifdef __sgi
{
std::ofstream tFile(m_FileName, std::ios::out);
tFile.close();
}
tmpWriteStream->open(m_FileName, std::ios::out);
#else
tmpWriteStream->open(m_FileName, std::ios::binary | std::ios::out);
#endif
tmpWriteStream->open(m_FileName.c_str(), std::ios::binary | std::ios::out);

if (!tmpWriteStream->rdbuf()->is_open())
{
Expand All @@ -795,7 +773,7 @@ MetaArray::Write(const char * _headName, const char * _dataName, bool _writeElem
}

bool
MetaArray::WriteStream(std::ofstream * _stream, bool _writeElements, const void * _constElementData)
MetaArray::WriteStream(METAIO_STREAM::ofstream * _stream, bool _writeElements, const void * _constElementData)
{
if (m_WriteStream != nullptr)
{
Expand Down Expand Up @@ -989,7 +967,7 @@ MetaArray::M_Read()
}

bool
MetaArray::M_ReadElements(std::ifstream * _fstream, void * _data, int _dataQuantity)
MetaArray::M_ReadElements(METAIO_STREAM::ifstream * _fstream, void * _data, int _dataQuantity)
{
META_DEBUG_PRINT( "MetaArray: M_ReadElements" );

Expand All @@ -1001,19 +979,27 @@ MetaArray::M_ReadElements(std::ifstream * _fstream, void * _data, int _dataQuant
// If compressed we inflate
if (m_CompressedData)
{
// if m_CompressedElementDataSize is not defined we assume the size of the
// file is the size of the compressed data
// if m_CompressedElementDataSize is not defined we assume the compressed
// data runs from the current position to the end of the file
if (m_CompressedElementDataSize == 0)
{
const std::streampos dataPos = _fstream->tellg();
_fstream->seekg(0, std::ios::end);
m_CompressedElementDataSize = _fstream->tellg();
_fstream->seekg(0, std::ios::beg);
m_CompressedElementDataSize = static_cast<std::streamoff>(_fstream->tellg() - dataPos);
_fstream->seekg(dataPos);
}

auto * compr = new unsigned char[static_cast<size_t>(m_CompressedElementDataSize)];
_fstream->read(reinterpret_cast<char *>(compr), static_cast<size_t>(m_CompressedElementDataSize));

MET_PerformUncompression(compr, m_CompressedElementDataSize, static_cast<unsigned char *>(_data), readSize);
const bool uncompressed =
MET_PerformUncompression(compr, m_CompressedElementDataSize, static_cast<unsigned char *>(_data), readSize);
delete[] compr;
if (!uncompressed)
{
std::cerr << "MetaArray: M_ReadElements: could not uncompress element data" << '\n';
return false;
}
}
else // if not compressed
{
Expand Down Expand Up @@ -1044,10 +1030,10 @@ MetaArray::M_ReadElements(std::ifstream * _fstream, void * _data, int _dataQuant
}

bool
MetaArray::M_WriteElements(std::ofstream * _fstream, const void * _data, std::streamoff _dataQuantity)
MetaArray::M_WriteElements(METAIO_STREAM::ofstream * _fstream, const void * _data, std::streamoff _dataQuantity)
{
bool localData;
std::ofstream * tmpWriteStream;
METAIO_STREAM::ofstream * tmpWriteStream;
if (m_ElementDataFileName == "LOCAL")
{
localData = true;
Expand All @@ -1056,7 +1042,7 @@ MetaArray::M_WriteElements(std::ofstream * _fstream, const void * _data, std::st
else
{
localData = false;
tmpWriteStream = new std::ofstream;
tmpWriteStream = new METAIO_STREAM::ofstream;

std::string dataFileName;
std::string pathName;
Expand All @@ -1070,17 +1056,7 @@ MetaArray::M_WriteElements(std::ofstream * _fstream, const void * _data, std::st
dataFileName = m_ElementDataFileName;
}

// Some older sgi compilers have a error in the ofstream constructor
// that requires a file to exist for output
#ifdef __sgi
{
std::ofstream tFile(dataFileName, std::ios::out);
tFile.close();
}
tmpWriteStream->open(dataFileName, std::ios::out);
#else
tmpWriteStream->open(dataFileName, std::ios::binary | std::ios::out);
#endif
tmpWriteStream->open(dataFileName.c_str(), std::ios::binary | std::ios::out);
}

if (!m_BinaryData)
Expand Down
10 changes: 5 additions & 5 deletions Modules/ThirdParty/MetaIO/src/MetaIO/src/metaArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ class METAIO_EXPORT MetaArray : public MetaForm
bool _autoFreeElementData = false);

virtual bool
CanReadStream(std::ifstream * _stream) const;
CanReadStream(METAIO_STREAM::ifstream * _stream) const;

virtual bool
ReadStream(std::ifstream * _stream,
ReadStream(METAIO_STREAM::ifstream * _stream,
bool _readElements = true,
void * _elementDataBuffer = nullptr,
bool _autoFreeElementData = false);
Expand All @@ -174,7 +174,7 @@ class METAIO_EXPORT MetaArray : public MetaForm
const void * _constElementData = nullptr);

virtual bool
WriteStream(std::ofstream * _stream, bool _writeElements = true, const void * _constElementData = nullptr);
WriteStream(METAIO_STREAM::ofstream * _stream, bool _writeElements = true, const void * _constElementData = nullptr);

// PROTECTED
protected:
Expand Down Expand Up @@ -205,10 +205,10 @@ class METAIO_EXPORT MetaArray : public MetaForm
M_Read() override;

bool
M_ReadElements(std::ifstream * _fstream, void * _data, int _dataQuantity);
M_ReadElements(METAIO_STREAM::ifstream * _fstream, void * _data, int _dataQuantity);

bool
M_WriteElements(std::ofstream * _fstream, const void * _data, std::streamoff _dataQuantity);
M_WriteElements(METAIO_STREAM::ofstream * _fstream, const void * _data, std::streamoff _dataQuantity);
};

# if (METAIO_USE_NAMESPACE)
Expand Down
Loading
Loading