From eefb010434cfa1ff98763df087efb3b7cfcc2ed7 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 16 Apr 2022 16:20:14 +0100 Subject: [PATCH] Apply @rcurtin modification to check STB version Signed-off-by: Omar Shrit --- CMake/TestForSTB.cmake | 37 ---------------------------------- CMake/TestStaticSTB.cmake | 42 +++++++++++++++++++++++++++++++++++++++ CMake/stb/CMakeLists.txt | 21 -------------------- CMake/stb/a.cpp | 15 ++++++++++++++ CMake/stb/a.hpp | 10 ++++++++++ CMake/stb/alib.cpp | 8 -------- CMake/stb/alib.hpp | 16 --------------- CMake/stb/b.cpp | 15 ++++++++++++++ CMake/stb/b.hpp | 10 ++++++++++ CMake/stb/blib.cpp | 8 -------- CMake/stb/blib.hpp | 16 --------------- CMake/stb/main.cpp | 19 +++++++++++------- CMakeLists.txt | 9 +++++++++ 13 files changed, 113 insertions(+), 113 deletions(-) delete mode 100644 CMake/TestForSTB.cmake create mode 100644 CMake/TestStaticSTB.cmake delete mode 100644 CMake/stb/CMakeLists.txt create mode 100644 CMake/stb/a.cpp create mode 100644 CMake/stb/a.hpp delete mode 100644 CMake/stb/alib.cpp delete mode 100644 CMake/stb/alib.hpp create mode 100644 CMake/stb/b.cpp create mode 100644 CMake/stb/b.hpp delete mode 100644 CMake/stb/blib.cpp delete mode 100644 CMake/stb/blib.hpp diff --git a/CMake/TestForSTB.cmake b/CMake/TestForSTB.cmake deleted file mode 100644 index 25fd8ee240..0000000000 --- a/CMake/TestForSTB.cmake +++ /dev/null @@ -1,37 +0,0 @@ -# Author: Omar Shrit - -#[=======================================================================[.rst: -TestForSTB --------------- - -Test to verify if the last version of STB that contains static -functions is available in the system - -check if the compiler supports the standard ANSI sstream header - -:: - - CMAKE_HAS_STATIC_STB - defined by the results -#]=======================================================================] - -if(NOT DEFINED CMAKE_HAS_STATIC_STB) - message(CHECK_START "Check for stb") - try_compile(CMAKE_HAS_STATIC_STB stb/main.cpp - OUTPUT_VARIABLE OUTPUT) - if (CMAKE_HAS_ANSI_STRING_STREAM) - message(CHECK_PASS "found") - set (CMAKE_NO_ANSI_STRING_STREAM 0 CACHE INTERNAL - "Does the compiler support sstream") - file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log - "Determining if the CXX compiler has sstream passed with " - "the following output:\n${OUTPUT}\n\n") - else () - message(CHECK_FAIL "not found") - set (CMAKE_NO_ANSI_STRING_STREAM 1 CACHE INTERNAL - "Does the compiler support sstream") - file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log - "Determining if the CXX compiler has sstream failed with " - "the following output:\n${OUTPUT}\n\n") - endif () -endif() - diff --git a/CMake/TestStaticSTB.cmake b/CMake/TestStaticSTB.cmake new file mode 100644 index 0000000000..b4b76adc63 --- /dev/null +++ b/CMake/TestStaticSTB.cmake @@ -0,0 +1,42 @@ +# Author: Omar Shrit + +#[=======================================================================[.rst: +TestForSTB +---------- + +Test to verify if the available version of STB contains a working static +implementation that can be used from multiple translation units. + +:: + + CMAKE_HAS_WORKING_STATIC_STB - defined by the results +#]=======================================================================] + +if(NOT DEFINED CMAKE_HAS_WORKING_STATIC_STB) + message(STATUS "Check that STB static implementation mode links correctly...") + try_compile(CMAKE_HAS_WORKING_STATIC_STB + ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/ + SOURCES + ${CMAKE_SOURCE_DIR}/CMake/stb/main.cpp + ${CMAKE_SOURCE_DIR}/CMake/stb/a.cpp + ${CMAKE_SOURCE_DIR}/CMake/stb/b.cpp + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${STB_IMAGE_INCLUDE_DIR}" + OUTPUT_VARIABLE out) + if (CMAKE_HAS_WORKING_STATIC_STB) + message(STATUS "Check that STB static implementation mode links " + "correctly... success") + set(CMAKE_HAS_WORKING_STATIC_STB 1 CACHE INTERNAL + "Does STB static implementation mode link correctly") + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log + "Determining if STB's static implementation can link correctly passed " + "with the following output:\n${out}\n\n") + else () + message(STATUS "Check that STB static implementation mode links " + "correctly... fail") + set(CMAKE_HAS_WORKING_STATIC_STB 0 CACHE INTERNAL + "Does STB static implementation mode link correctly") + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "Determining if STB's static implementation can link correctly failed " + "with the following output:\n${out}\n\n") + endif () +endif() diff --git a/CMake/stb/CMakeLists.txt b/CMake/stb/CMakeLists.txt deleted file mode 100644 index 0ea752cb61..0000000000 --- a/CMake/stb/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -cmake_minimum_required(VERSION 3.9) -project(CheckSTB) -include(GNUInstallDirs) - -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -add_library(alib SHARED alib.hpp alib.cpp) -add_library(blib SHARED blib.hpp blib.cpp) - -#set(alib "alib.so") -#set(blib "libblib.so") - -add_executable(CheckSTB - main.cpp - ) - -target_link_libraries(CheckSTB - alib - blib - ) diff --git a/CMake/stb/a.cpp b/CMake/stb/a.cpp new file mode 100644 index 0000000000..4a99350577 --- /dev/null +++ b/CMake/stb/a.cpp @@ -0,0 +1,15 @@ +#include "a.hpp" + +// Include the static implementation of all STB functions. +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_WRITE_STATIC +#define STB_IMAGE_WRITE_IMPLEMENTATION + +#include +#include + +void A::A() +{ + // Do nothing, just to check if the STB library is a working version. +} diff --git a/CMake/stb/a.hpp b/CMake/stb/a.hpp new file mode 100644 index 0000000000..be30807b5e --- /dev/null +++ b/CMake/stb/a.hpp @@ -0,0 +1,10 @@ +#ifndef A_HPP +#define A_HPP + +namespace A { + +void A(); + +} + +#endif diff --git a/CMake/stb/alib.cpp b/CMake/stb/alib.cpp deleted file mode 100644 index e92d7402bc..0000000000 --- a/CMake/stb/alib.cpp +++ /dev/null @@ -1,8 +0,0 @@ - - -#include "alib.hpp" - -void Alib::A() -{ - //Do nothing, just to check if the STB library has the good version. -} diff --git a/CMake/stb/alib.hpp b/CMake/stb/alib.hpp deleted file mode 100644 index ee36e8474e..0000000000 --- a/CMake/stb/alib.hpp +++ /dev/null @@ -1,16 +0,0 @@ - -#ifndef ALIB_HPP -#define ALIB_HPP - -#define STB_IMAGE_WRITE_STATIC -#define STB_IMAGE_WRITE_IMPLEMENTATION - -#include - -namespace Alib { - -void A(); - -} - -#endif diff --git a/CMake/stb/b.cpp b/CMake/stb/b.cpp new file mode 100644 index 0000000000..a11a8f15e7 --- /dev/null +++ b/CMake/stb/b.cpp @@ -0,0 +1,15 @@ +#include "b.hpp" + +// Include the static implementation of all STB functions. +#define STB_IMAGE_STATIC +#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_WRITE_STATIC +#define STB_IMAGE_WRITE_IMPLEMENTATION + +#include +#include + +void B::B() +{ + // Do nothing, just to check if the STB library is a working version. +} diff --git a/CMake/stb/b.hpp b/CMake/stb/b.hpp new file mode 100644 index 0000000000..0a287e6b17 --- /dev/null +++ b/CMake/stb/b.hpp @@ -0,0 +1,10 @@ +#ifndef B_HPP +#define B_HPP + +namespace B { + +void B(); + +} + +#endif diff --git a/CMake/stb/blib.cpp b/CMake/stb/blib.cpp deleted file mode 100644 index 14b6eb3025..0000000000 --- a/CMake/stb/blib.cpp +++ /dev/null @@ -1,8 +0,0 @@ - - -#include "blib.hpp" - -void Blib::B() -{ - //Do nothing, just to check if the STB library has the good version. -} diff --git a/CMake/stb/blib.hpp b/CMake/stb/blib.hpp deleted file mode 100644 index 43cc547322..0000000000 --- a/CMake/stb/blib.hpp +++ /dev/null @@ -1,16 +0,0 @@ - -#ifndef BLIB_HPP -#define BLIB_HPP - -#define STB_IMAGE_STATIC -#define STB_IMAGE_IMPLEMENTATION - -#include - -namespace Blib { - -void B(); - -} - -#endif diff --git a/CMake/stb/main.cpp b/CMake/stb/main.cpp index 3790075942..02cf644606 100644 --- a/CMake/stb/main.cpp +++ b/CMake/stb/main.cpp @@ -1,11 +1,16 @@ - -#include "alib.hpp" -#include "blib.hpp" +// The purpose of this file is to include STB's implementation in two separate +// translation units. One is a.cpp, and one is b.cpp. This file simply +// includes both of those, so that when we get to the linking phase, we will +// have to link both translation units. +// +// Some versions of STB fail to correctly define some functions as +// static---which will cause a linking failure. Thus, if this fails to +// compile, then mlpack's use of STB will fail. +#include "a.hpp" +#include "b.hpp" int main() { - - Alib::A(); - - Blib::B(); + A::A(); + B::B(); } diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a04283031..7faa21b8d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -325,6 +325,15 @@ if (STB_IMAGE_FOUND) add_definitions(-DHAS_STB) set(STB_AVAILABLE "1") set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} "${STB_IMAGE_INCLUDE_DIR}") + + # Make sure that we can link STB in multiple translation units. + include(CMake/TestStaticSTB.cmake) + if (NOT CMAKE_HAS_WORKING_STATIC_STB) + message(FATAL_ERROR "STB implementations's static mode cannot link across " + "multiple translation units! Try upgrading your STB implementation, " + "or using the auto-downloader (set DOWNLOAD_DEPENDENCIES=ON in the " + "CMake configuration command.") + endif () endif() # Find ensmallen.