Skip to content

Commit

Permalink
add support for building on windows platform
Browse files Browse the repository at this point in the history
cmake -G "MinGW Makefiles" -DUHDR_BUILD_TESTS=1 ../
cmake --build ./
  • Loading branch information
ram-mohan committed Nov 1, 2023
1 parent 5be8a63 commit 466ffc0
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 146 deletions.
84 changes: 50 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ project(UltraHdr C CXX)
# Detect system
###########################################################
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
elseif(WIN32)
elseif(APPLE)
else()
message(FATAL_ERROR "Platform not supported")
Expand Down Expand Up @@ -84,39 +85,50 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_compile_options(-ffunction-sections)
add_compile_options(-fdata-sections)
add_compile_options(-fomit-frame-pointer)
if(ARCH STREQUAL "x86")
add_compile_options(-m32)
add_compile_options(-march=pentium4)
add_compile_options(-mtune=generic)
endif()
if(ARCH STREQUAL "x86_64")
add_compile_options(-m64)
add_compile_options(-march=x86-64)
add_compile_options(-mtune=generic)
endif()

include(CheckCXXCompilerFlag)
function(CheckCompilerOption opt res)
set(CMAKE_REQUIRED_FLAGS ${opt})
check_cxx_compiler_flag(${opt} ${res})
unset(CMAKE_REQUIRED_FLAGS)
if(NOT ${res})
message(FATAL_ERROR "Unsupported compiler option(s) ${opt}")
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
# Disable specific warnings
# TODO: None of these should be disabled, but for now,for a warning-free msvc build these are
# added. fix the warnings and remove these filters
add_compile_options(/wd4244) # conversion from 'type1' to 'type2', possible loss of data
add_compile_options(/wd4267) # conversion from 'size_t' to 'type' possible loss of data
add_compile_options(/wd4305) # truncation from 'double' to 'float'
add_compile_options(/wd4838) # conversion from 'type1' to 'type2' requires a narrowing conversion
else()
add_compile_options(-ffunction-sections)
add_compile_options(-fdata-sections)
add_compile_options(-fomit-frame-pointer)
if(ARCH STREQUAL "x86")
add_compile_options(-m32)
add_compile_options(-march=pentium4)
add_compile_options(-mtune=generic)
endif()
if(ARCH STREQUAL "x86_64")
add_compile_options(-m64)
add_compile_options(-march=x86-64)
add_compile_options(-mtune=generic)
endif()
endfunction(CheckCompilerOption)

if(DEFINED UHDR_SANITIZE_OPTIONS)
CheckCompilerOption("-fsanitize=${UHDR_SANITIZE_OPTIONS}" SUPPORTS_SAN_OPTIONS)
add_compile_options(-fsanitize=${UHDR_SANITIZE_OPTIONS})
add_link_options(-fsanitize=${UHDR_SANITIZE_OPTIONS})
endif()
include(CheckCXXCompilerFlag)
function(CheckCompilerOption opt res)
set(CMAKE_REQUIRED_FLAGS ${opt})
check_cxx_compiler_flag(${opt} ${res})
unset(CMAKE_REQUIRED_FLAGS)
if(NOT ${res})
message(FATAL_ERROR "Unsupported compiler option(s) ${opt}")
endif()
endfunction(CheckCompilerOption)

if(UHDR_BUILD_FUZZERS)
CheckCompilerOption("-fsanitize=fuzzer-no-link" fuzz)
add_compile_options(-fsanitize=fuzzer-no-link)
if(DEFINED UHDR_SANITIZE_OPTIONS)
CheckCompilerOption("-fsanitize=${UHDR_SANITIZE_OPTIONS}" SUPPORTS_SAN_OPTIONS)
add_compile_options(-fsanitize=${UHDR_SANITIZE_OPTIONS})
add_link_options(-fsanitize=${UHDR_SANITIZE_OPTIONS})
endif()

if(UHDR_BUILD_FUZZERS)
CheckCompilerOption("-fsanitize=fuzzer-no-link" fuzz)
add_compile_options(-fsanitize=fuzzer-no-link)
endif()
endif()

if(UHDR_ENABLE_LOGS)
Expand Down Expand Up @@ -228,10 +240,14 @@ if(UHDR_BUILD_TESTS)
target_link_options(ultrahdr_unit_test PRIVATE -fsanitize=fuzzer-no-link)
endif()
target_link_libraries(ultrahdr_unit_test ultrahdr ${GTEST_BOTH_LIBRARIES})
execute_process(COMMAND cmake -E create_symlink
"${TESTS_DIR}/data/"
"${CMAKE_CURRENT_BINARY_DIR}/data"
)
if(WIN32)
file(COPY "${TESTS_DIR}/data/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/data")
else()
execute_process(COMMAND cmake -E create_symlink
"${TESTS_DIR}/data/"
"${CMAKE_CURRENT_BINARY_DIR}/data"
)
endif()
add_test(NAME UHDRUnitTests, COMMAND ultrahdr_unit_test)
endif()

Expand Down
30 changes: 27 additions & 3 deletions examples/ultrahdr_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
* limitations under the License.
*/

#ifdef _MSC_VER
#include <Windows.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif

#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -57,7 +61,26 @@ const float BT2020RGBtoYUVMatrix[9] = {0.2627,
(-0.6780 / 1.4746),
(-0.0593 / 1.4746)};

//#define PROFILE_ENABLE 1
#ifdef _MSC_VER
class Profiler {
public:
void timerStart() { QueryPerformanceCounter(&mStartingTime); }

void timerStop() { QueryPerformanceCounter(&mEndingTime); }

int64_t elapsedTime() {
LARGE_INTEGER frequency;
LARGE_INTEGER elapsedMicroseconds;
QueryPerformanceFrequency(&frequency);
elapsedMicroseconds.QuadPart = mEndingTime.QuadPart - mStartingTime.QuadPart;
return (double)elapsedMicroseconds.QuadPart / (double)frequency.QuadPart * 1000000;
}

private:
LARGE_INTEGER mStartingTime;
LARGE_INTEGER mEndingTime;
};
#else
class Profiler {
public:
void timerStart() { gettimeofday(&mStartingTime, nullptr); }
Expand All @@ -75,6 +98,7 @@ class Profiler {
struct timeval mStartingTime;
struct timeval mEndingTime;
};
#endif

static bool loadFile(const char* filename, void*& result, int length) {
std::ifstream ifd(filename, std::ios::binary | std::ios::ate);
Expand Down Expand Up @@ -247,8 +271,8 @@ bool UltraHdrAppInput::encode() {
if (mYuv420File != nullptr && !fillYuv420ImageHandle()) return false;
if (mYuv420JpegFile != nullptr && !fillYuv420JpegImageHandle()) return false;

mJpegImgR.maxLength = std::max(static_cast<size_t>(8 * 1024) /* min size 8kb */,
mRawP010Image.width * mRawP010Image.height * 3 * 2);
mJpegImgR.maxLength = (std::max)(static_cast<size_t>(8 * 1024) /* min size 8kb */,
mRawP010Image.width * mRawP010Image.height * 3 * 2);
mJpegImgR.data = malloc(mJpegImgR.maxLength);
if (mJpegImgR.data == nullptr) {
std::cerr << "unable to allocate memory to store compressed image" << std::endl;
Expand Down
Loading

0 comments on commit 466ffc0

Please sign in to comment.