Skip to content

Commit

Permalink
various fixes for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
scholarsmate committed Nov 20, 2023
1 parent a95ec66 commit f9dd853
Show file tree
Hide file tree
Showing 25 changed files with 527 additions and 371 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Setup cmake 🔧
uses: lukka/get-cmake@latest

- name: Prepare, Build, and Install Ωedit 🔧
- name: Prepare, Build, Test, and Install Ωedit 🔧
run: |
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON --install-prefix "${PWD}/_install"
cmake --build build --config Release
Expand Down Expand Up @@ -85,21 +85,21 @@ jobs:
- name: Make _install/lib directory to store lib files 🔧
run: mkdir -p _install/lib

- name: Download macos library file 🔻
- name: Download macOS library file 🔻
uses: actions/download-artifact@v3
if: runner.os == 'macOS'
with:
name: libomega_edit.dylib
path: _install/lib/libomega_edit.dylib

- name: Download linux library file 🔻
- name: Download Linux library file 🔻
uses: actions/download-artifact@v3
if: runner.os == 'Linux'
with:
name: libomega_edit.so
path: _install/lib/libomega_edit.so

- name: Download windows library file 🔻
- name: Download Windows library file 🔻
uses: actions/download-artifact@v3
if: runner.os == 'Windows'
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ _install
_install*/
build*/
cmake-build*/
core/src/include/omega-edit/features.h
node_modules/
venv/
package-lock.json
Expand Down
7 changes: 4 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ for objtype in shared static; do
ctest -C "$type" --test-dir "build-$objtype-$type/core" --output-on-failure
cmake --install "build-$objtype-$type/packages/core" --prefix "$install_dir-$objtype-$type" --config "$type"
cpack --config "build-$objtype-$type/CPackSourceConfig.cmake"
cpack --config "build-$objtype-$type/CPackConfig.cmake"
cpack --config "build-$objtype-$type/CPackConfig.cmake" -C "$type"
done

# used by scala native code to bundle the proper library file
Expand All @@ -48,8 +48,9 @@ yarn install
yarn lint

if [[ -d "$OE_LIB_DIR" ]]; then
rm -f _install
ln -s "$OE_LIB_DIR" _install
rm -rf _install
# Use cp instead of a symlink so it works on Windows
cp -a "$OE_LIB_DIR" _install
fi

# Build, test, and package Scala server node module
Expand Down
34 changes: 21 additions & 13 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (DEFINED omega_edit_SHARED_LIBS)
set(BUILD_SHARED_LIBS "${omega_edit_SHARED_LIBS}")
endif ()

message(STATUS "Building ${PROJECT_NAME} ${PROJECT_VERSION} (shared libs: ${BUILD_SHARED_LIBS})")
message(STATUS "Building ${PROJECT_NAME} ${PROJECT_VERSION} (shared: ${BUILD_SHARED_LIBS}, tests: ${BUILD_TESTS}, docs: ${BUILD_DOCS}, examples: ${BUILD_EXAMPLES})")

# Common configurations
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
Expand All @@ -32,17 +32,12 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT MINGW)
set(FILESYSTEM_LIB stdc++fs)
endif ()

# Dependencies
if (BUILD_TESTS)
include(FetchContent)
FetchContent_Declare(
Catch2
GIT_SHALLOW TRUE
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH "${Catch2_SOURCE_DIR}/extras")
endif ()
include(CheckFunctionExists)
# Check platform features
check_function_exists(fseeko HAVE_FSEEKO)
check_function_exists(ftello HAVE_FTELLO)
check_function_exists(fopen_s HAVE_FOPEN_S)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/features.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/src/include/omega_edit/features.h")

# Core library configuration
file(GLOB_RECURSE OMEGA_EDIT_SOURCE_FILES "src/include/*.h" "src/include/*.hpp" "src/lib/*.c" "src/lib/*.cpp")
Expand Down Expand Up @@ -72,9 +67,22 @@ endif ()
# Tests
if (BUILD_TESTS)
enable_testing()
include(FetchContent)
# Force Catch2 to build statically so we don't have to worry about the path to the Catch2 DDL when running tests
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
FetchContent_Declare(
Catch2
GIT_SHALLOW TRUE
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0
)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH "${Catch2_SOURCE_DIR}/extras")
add_executable(omega_test "src/tests/omega_test.cpp" "src/tests/test_util.h")
target_link_libraries(omega_test PRIVATE omega_edit::omega_edit Catch2::Catch2WithMain ${FILESYSTEM_LIB})
add_custom_command(TARGET omega_test POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/src/tests/data" "$<TARGET_FILE_DIR:omega_test>/data")
# Set the runtime output directory
set_target_properties(omega_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
add_custom_command(TARGET omega_test POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/src/tests/data" "${CMAKE_CURRENT_BINARY_DIR}/data")
include(CTest)
include(Catch)
catch_discover_tests(omega_test WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
Expand Down
2 changes: 1 addition & 1 deletion core/src/examples/play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void session_change_cbk(const omega_session_t *session_ptr, omega_session_event_
case SESSION_EVT_EDIT: {
auto file_info_ptr = (file_info_t *) omega_session_get_user_data_ptr(session_ptr);
file_info_ptr->deletes = file_info_ptr->inserts = file_info_ptr->overwrites = 0;
file_info_ptr->save_fptr = fopen(file_info_ptr->save_filename, "wb");
file_info_ptr->save_fptr = FOPEN(file_info_ptr->save_filename, "wb");
omega_visit_changes(session_ptr, save_changes_cbk, file_info_ptr);
fclose(file_info_ptr->save_fptr);
}
Expand Down
59 changes: 54 additions & 5 deletions core/src/include/omega_edit/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
#ifndef OMEGA_EDIT_CONFIG_H
#define OMEGA_EDIT_CONFIG_H

#include <omega_edit/features.h> // this header is generated at build time
#include <fcntl.h>

#ifdef __cplusplus

#include <cstdint>
#include <cstdio>

#else

#include <stdint.h>
#include <stdio.h>

#endif//__cplusplus

Expand Down Expand Up @@ -71,15 +76,59 @@
#error Unknown pointer size or missing size macros!
#endif

#ifdef _MSC_VER
#include <io.h>
// For MSVC, use _sopen_s
static inline int safe_open_(const char* filename, int oflag, int pmode) {
int fd;
_sopen_s(&fd, filename, oflag | _O_BINARY, _SH_DENYWR, pmode);
return fd;
}
#else
// For other compilers/platforms, fall back to open
static inline int safe_open_(const char* filename, int oflag, int pmode) {
// Note: The mode only applies if O_CREAT is part of oflag
return open(filename, oflag, pmode);
}
#endif

/**
* @brief Alias for the open function, accommodating large files if _LARGEFILE_SOURCE is defined.
*/
#ifndef OPEN
#ifdef _LARGEFILE_SOURCE
#define OPEN open
#define OPEN safe_open_
#endif

/**
* @brief Function to safely open a file pointer, using fopen_s where supported.
* @param filename file name to open
* @param mode mode to open the file in
* @return opened file pointer
*/
static inline FILE* safe_fopen_(const char* filename, const char* mode) {
FILE* file;
#ifdef HAVE_FOPEN_S
// Use fopen_s where supported
fopen_s(&file, filename, mode);
#else
#define OPEN open
// Fallback for compilers that don't support fopen_s
file = fopen(filename, mode);
#endif
return file;
}

/**
* Alias for the fopen function used to open a file pointer.
*/
#ifndef FOPEN
#define FOPEN safe_fopen_
#endif

/**
* Alias for the fclose function used to close a file pointer.
*/
#ifndef FCLOSE
#define FCLOSE fclose
#endif

/**
Expand All @@ -93,7 +142,7 @@
* Alias for the fseek/fseeko function, using fseeko if _LARGEFILE_SOURCE is defined to accommodate large files.
*/
#ifndef FSEEK
#ifdef _LARGEFILE_SOURCE
#ifdef HAVE_FSEEKO
#define FSEEK fseeko
#else
#define FSEEK fseek
Expand All @@ -104,7 +153,7 @@
* Alias for the ftell/ftello function, using ftello if _LARGEFILE_SOURCE is defined to accommodate large files.
*/
#ifndef FTELL
#ifdef _LARGEFILE_SOURCE
#ifdef HAVE_FTELLO
#define FTELL ftello
#else
#define FTELL ftell
Expand Down
17 changes: 17 additions & 0 deletions core/src/include/omega_edit/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ extern "C" {

#endif

/**
* Generate a temporary file name based on tmpl. The name constructed does not exist at the time of the call.
* The tmpl parameter is overwritten with the result.
* @param tmpl must match the rules for mk[s]temp (i.e. end in "XXXXXX")
* @param mode mode to set the file to, if zero then the mode is set to 0600 modulo umask
* @return read-write file descriptor opened with mode 0600 modulo umask or -1 with errno set on error
*/
int omega_util_mkstemp(char *tmpl, int mode);

/**
* Gets the current working directory
* @param buffer pointer to memory to hold the current working directory (allocated to at least FILENAME_MAX) or could be NULL, in which case an internal static buffer will be used
Expand Down Expand Up @@ -95,6 +104,14 @@ int64_t omega_util_file_size(char const *path);
*/
int omega_util_paths_equivalent(char const *path1, char const *path2);

/**
* Compare the contents of two files
* @param path1 first path
* @param path2 second path
* @return 0 if the contents are equal, 1 if the contents are not equal, or -1 if an error occurred opening the file at path1 or -2 if an error occurred opening the file at path2
*/
int omega_util_compare_files(const char* path1, const char* path2);

/**
* Compare the modification times of two files
* @param path1 first path
Expand Down
9 changes: 0 additions & 9 deletions core/src/include/omega_edit/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ extern "C" {
*/
int omega_util_compute_mode(int mode);

/**
* Generate a temporary file name based on tmpl. The name constructed does not exist at the time of the call.
* The tmpl parameter is overwritten with the result.
* @param tmpl must match the rules for mk[s]temp (i.e. end in "XXXXXX")
* @param mode mode to set the file to, if zero then the mode is set to 0600 modulo umask
* @return read-write file descriptor opened with mode 0600 modulo umask or -1 with errno set on error
*/
int omega_util_mkstemp(char *tmpl, int mode);

/**
* Write a segment from one file into another file
* @param from_file_ptr from file pointer, opened for read
Expand Down
Loading

0 comments on commit f9dd853

Please sign in to comment.