Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code and build system to build on Visual C++ (#25) #34

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
52 changes: 39 additions & 13 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jobs:
build_type: RelWithDebInfo,
gtk_tsm: "Off"
}
- {
name: "Windows",
os: windows-latest,
build_type: Release,
gtk_tsm: "Off"
}

steps:
- uses: actions/checkout@v2
Expand All @@ -35,26 +41,46 @@ jobs:
echo github.event.action: ${{ github.event.action }}
echo github.event_name: ${{ github.event_name }}

- name: Install Conan
run: pip3 install conan

- name: Install dependencies on Ubuntu
if: startsWith(matrix.config.name, 'Ubuntu')
run: sudo apt-get install -y check valgrind libgtk-3-dev libpango1.0-dev pkg-config
run: sudo apt-get install -y valgrind libgtk-3-dev libpango1.0-dev pkg-config

- name: Install dependencies on macOS
if: startsWith(matrix.config.name, 'macOS')
run: brew install check
- name: Create build directory on Unix
if: ${{ !startsWith(matrix.config.name, 'Windows') }}
run: mkdir build

- name: Configure CMake on Ubuntu
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ./build -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} -DBUILD_TESTING=On -DBUILD_GTKTSM=${{ matrix.config.gtk_tsm }}
- name: Create build directory on Windows
if: startsWith(matrix.config.name, 'Windows')
run: md build

- name: Build
# Build your program with the given configuration
run: cmake --build ./build --config ${{ matrix.config.build_type }}
- name: Install libcheck
working-directory: ./build
run: conan install .. --build=missing

- name: Configure CMake on Unix
if: ${{ !startsWith(matrix.config.name, 'Windows') }}
working-directory: ./build
run: cmake .. -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} -DBUILD_TESTING=On -DBUILD_GTKTSM=${{ matrix.config.gtk_tsm }}

- name: Configure CMake on Windows
if: startsWith(matrix.config.name, 'Windows')
working-directory: ./build
run: cmake .. -G "Visual Studio 17" -DBUILD_SHARED_LIBS=Off -DBUILD_TESTING=On -DBUILD_GTKTSM=${{ matrix.config.gtk_tsm }}

- name: Build on Unix
if: ${{ !startsWith(matrix.config.name, 'Windows') }}
working-directory: ./build
run: cmake --build .

- name: Build on Windows
if: startsWith(matrix.config.name, 'Windows')
working-directory: ./build
run: cmake --build . --config ${{ matrix.config.build_type }}
Comment on lines +63 to +81
Copy link
Owner

Choose a reason for hiding this comment

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

Almost every step is conditioned on Windows or not. IMHO it's cleaner to just create a separate Windows build job.


- name: Test
working-directory: ./build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{ matrix.config.build_type }}

7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "Usage: mkdir build; cmake ..")
endif()

# Search in the build dir for Find*.cmake files provided by Conan
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})

# Include utilities
include(cmake/Utilities.cmake)
include(cmake/JoinPaths.cmake)
Expand Down Expand Up @@ -102,8 +105,8 @@ endif()

if(BUILD_TESTING)
enable_testing()
find_package(Check)
set_package_properties(Check PROPERTIES
find_package(check REQUIRED)
set_package_properties(check PROPERTIES
TYPE REQUIRED
PURPOSE "For testing"
)
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ This is a personal modified version. For more information, please refer to its o
[91335]: https://bugs.freedesktop.org/show_bug.cgi?id=91335

## Build

On Linux and macOS:

```bash
mkdir build && cd build
cmake ..
make
make install
```

On Windows with Visual Studio 2022 (in a x64 Native Tools prompt):

```bash
md build
cd build
cmake .. -G "Visual Studio 17"
cmake --build . --config Debug
cmake --install . --prefix YOUR_TARGET_DIR
```

### Build options
Options may be supplied when configuring cmake:
```bash
Expand Down
22 changes: 13 additions & 9 deletions cmake/CompileOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,26 @@ endif()
function(add_libtsm_compile_options target)
# Make all files include "config.h" by default. This shouldn't cause any
# problems and we cannot forget to include it anymore.
target_compile_options(${target} PRIVATE
-include ${CMAKE_BINARY_DIR}/config.h
-pipe
-fno-common
-ffast-math
-fno-strict-aliasing
-ffunction-sections
-fdata-sections
)
if(NOT MSVC)
target_compile_options(${target} PRIVATE
-include ${CMAKE_BINARY_DIR}/config.h
-pipe
-fno-common
-ffast-math
-fno-strict-aliasing
-ffunction-sections
-fdata-sections
)
endif()

# Linker flags
## Make the linker discard all unused symbols.
if(APPLE)
set(LDFLAGS "-Wl,-dead_strip -Wl,-dead_strip_dylibs -Wl,-bind_at_load")
elseif(UNIX)
set(LDFLAGS "-Wl,--as-needed -Wl,--gc-sections -Wl,-z,relro -Wl,-z,now")
elseif(MSVC)
set(LDFLAGS "/OPT:REF")
else()
message("Unsupported platform, you are on your own.")
endif()
Expand Down
8 changes: 8 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[requires]
libcheck/0.15.2

[generators]
cmake_find_package

[options]
libcheck:with_subunit=False
4 changes: 4 additions & 0 deletions src/shared/shl-htable.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
#include <string.h>
#include "shl-htable.h"

#ifdef _MSC_VER
#define COLD
#else
#define COLD __attribute__((cold))
#endif

struct htable {
/* KEEP IN SYNC WITH "struct shl_htable_int" */
Expand Down
5 changes: 1 addition & 4 deletions src/shared/shl-htable.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@

/* miscellaneous */

#define shl_htable_offsetof(pointer, type, member) ({ \
const typeof(((type*)0)->member) *__ptr = (pointer); \
(type*)(((char*)__ptr) - offsetof(type, member)); \
})
#define shl_htable_offsetof(pointer, type, member) ((type*)(((char*) pointer) - offsetof(type, member)))

/* htable */

Expand Down
14 changes: 11 additions & 3 deletions src/shared/shl-llog.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
#include <stdbool.h>
#include <stdlib.h>

#ifdef _MSC_VER
#define UNUSED
#define VALIDATE_PRINTF(a,b)
#else
#define UNUSED __attribute__((__unused__))
#define VALIDATE_PRINTF(a,b) __attribute__((format(printf, (a), (b))))
#endif

enum llog_severity {
LLOG_FATAL = 0,
LLOG_ALERT = 1,
Expand All @@ -81,7 +89,7 @@ typedef void (*llog_submit_t) (void *data,
const char *format,
va_list args);

static inline __attribute__((format(printf, 8, 9)))
static inline VALIDATE_PRINTF(8, 9)
void llog_format(llog_submit_t llog,
void *data,
const char *file,
Expand All @@ -104,7 +112,7 @@ void llog_format(llog_submit_t llog,
}

#ifndef LLOG_SUBSYSTEM
static const char *LLOG_SUBSYSTEM __attribute__((__unused__));
static const char *LLOG_SUBSYSTEM UNUSED;
#endif

#define LLOG_DEFAULT __FILE__, __LINE__, __func__, LLOG_SUBSYSTEM
Expand All @@ -124,7 +132,7 @@ static const char *LLOG_SUBSYSTEM __attribute__((__unused__));
(format), \
##__VA_ARGS__)

static inline __attribute__((format(printf, 4, 5)))
static inline VALIDATE_PRINTF(4, 5)
void llog_dummyf(llog_submit_t llog, void *data, unsigned int sev,
const char *format, ...)
{
Expand Down
4 changes: 4 additions & 0 deletions src/tsm/libtsm-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
#include "libtsm.h"
#include "shl-llog.h"

#ifdef _MSC_VER
#define SHL_EXPORT
#else
#define SHL_EXPORT __attribute__((visibility("default")))
#endif

/* max combined-symbol length */
#define TSM_UCS4_MAXLEN 10
Expand Down
56 changes: 54 additions & 2 deletions src/tsm/tsm-screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,19 @@ static void screen_scroll_up(struct tsm_screen *con, unsigned int num)
* also be small enough so we do not get stack overflows. */
if (num > 128) {
screen_scroll_up(con, 128);
return screen_scroll_up(con, num - 128);
screen_scroll_up(con, num - 128);
return;
}

#ifdef _MSC_VER
struct line **cache = malloc(sizeof(struct line *) * num);
if (!cache) {
llog_warning(con, "Out of memory");
return;
}
#else
struct line *cache[num];
#endif
Comment on lines +290 to +298
Copy link
Owner

Choose a reason for hiding this comment

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

Using non-constant variables as array size is nonstandard anyway. Let's just use the malloc version on all platforms.


for (i = 0; i < num; ++i) {
pos = con->margin_top + i;
Expand Down Expand Up @@ -332,6 +342,10 @@ static void screen_scroll_up(struct tsm_screen *con, unsigned int num)
}
}
}

#ifdef _MSC_VER
free(cache);
#endif
}

static void screen_scroll_down(struct tsm_screen *con, unsigned int num)
Expand All @@ -351,9 +365,19 @@ static void screen_scroll_down(struct tsm_screen *con, unsigned int num)
/* see screen_scroll_up() for an explanation */
if (num > 128) {
screen_scroll_down(con, 128);
return screen_scroll_down(con, num - 128);
screen_scroll_down(con, num - 128);
return;
}

#ifdef _MSC_VER
struct line **cache = malloc(sizeof(struct line *) * num);
if (!cache) {
llog_warning(con, "Out of memory");
return;
}
#else
struct line *cache[num];
#endif

for (i = 0; i < num; ++i) {
cache[i] = con->lines[con->margin_bottom - i];
Expand All @@ -376,6 +400,10 @@ static void screen_scroll_down(struct tsm_screen *con, unsigned int num)
if (!con->sel_end.line && con->sel_end.y >= 0)
con->sel_end.y += num;
}

#ifdef _MSC_VER
free(cache);
#endif
}

static void screen_write(struct tsm_screen *con, unsigned int x,
Expand Down Expand Up @@ -1360,7 +1388,15 @@ void tsm_screen_insert_lines(struct tsm_screen *con, unsigned int num)
if (num > max)
num = max;

#ifdef _MSC_VER
struct line **cache = malloc(sizeof(struct line *) * num);
if (!cache) {
llog_warning(con, "Out of memory");
return;
}
#else
struct line *cache[num];
#endif

for (i = 0; i < num; ++i) {
cache[i] = con->lines[con->margin_bottom - i];
Expand All @@ -1378,6 +1414,10 @@ void tsm_screen_insert_lines(struct tsm_screen *con, unsigned int num)
}

con->cursor_x = 0;

#ifdef _MSC_VER
free(cache);
#endif
}

SHL_EXPORT
Expand All @@ -1400,7 +1440,15 @@ void tsm_screen_delete_lines(struct tsm_screen *con, unsigned int num)
if (num > max)
num = max;

#ifdef _MSC_VER
struct line **cache = malloc(sizeof(struct line *) * num);
if (!cache) {
llog_warning(con, "Out of memory");
return;
}
#else
struct line *cache[num];
#endif

for (i = 0; i < num; ++i) {
cache[i] = con->lines[con->cursor_y + i];
Expand All @@ -1418,6 +1466,10 @@ void tsm_screen_delete_lines(struct tsm_screen *con, unsigned int num)
}

con->cursor_x = 0;

#ifdef _MSC_VER
free(cache);
#endif
}

SHL_EXPORT
Expand Down
Loading