Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

Hello world example #23

Merged
merged 8 commits into from
Jul 2, 2022
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "ext/flecs"]
path = ext/flecs
url = https://github.com/SanderMertens/flecs
[submodule "ext/scripts"]
path = ext/scripts
url = https://github.com/bottlenoselabs/scripts.git
5 changes: 4 additions & 1 deletion bindgen/ast_linux.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"directory": "./ast",
"c": {
"input_file": "../ext/flecs/include/flecs.h",
"input_file": "../src/c/production/flecs/include/flecs_pinvoke.h",
"user_include_directories": [
"../ext/flecs/include"
],
"platforms": {
"aarch64-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {}
Expand Down
5 changes: 4 additions & 1 deletion bindgen/ast_osx.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"directory": "./ast",
"c": {
"input_file": "../ext/flecs/include/flecs.h",
"input_file": "../src/c/production/flecs/include/flecs_pinvoke.h",
"user_include_directories": [
"../ext/flecs/include"
],
"platforms": {
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {}
Expand Down
5 changes: 4 additions & 1 deletion bindgen/ast_win.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"directory": "./ast",
"c": {
"input_file": "../ext/flecs/include/flecs.h",
"input_file": "../src/c/production/flecs/include/flecs_pinvoke.h",
"user_include_directories": [
"../ext/flecs/include"
],
"platforms": {
"aarch64-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {}
Expand Down
1 change: 1 addition & 0 deletions ext/scripts
Submodule scripts added at d285ce
3 changes: 2 additions & 1 deletion flecs.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
<_LibraryDirectoryPath>$(_GitDirectoryPath)/lib</_LibraryDirectoryPath>
<_SourceCodeDirectoryPath>$(_GitDirectoryPath)/src/cs/production/$(_LibraryName)/</_SourceCodeDirectoryPath>
<_LinkDirectoryPath>$(_LibraryName)/</_LinkDirectoryPath>
<_IsEnabledSourceCode Condition=" '$(_IsEnabledSourceCode)' == '' ">true</_IsEnabledSourceCode>
</PropertyGroup>

<!-- C# source code-->
<ItemGroup>
<ItemGroup Condition=" '$(_IsEnabledSourceCode)' == 'true' ">
<Compile Include="$(_SourceCodeDirectoryPath)/**/*.cs" >
<Pack>false</Pack>
<Link>$(_LinkDirectoryPath)/%(RecursiveDir)%(Filename)%(Extension)</Link>
Expand Down
140 changes: 7 additions & 133 deletions library.sh
Original file line number Diff line number Diff line change
@@ -1,136 +1,10 @@
#!/bin/bash
# NOTE: This script builds a target C/C++ library using CMake as a shared library (`.dll`/`.so`/`.dylib`) for the purposes of P/Invoke with C#.
# INPUT:
# $1: The target operating system to build the shared library for. Possible values are "host", "windows", "linux", "macos".
# $2: The taget architecture to build the shared library for. Possible values are "default", "x86_64", "arm64".
# OUTPUT: The built shared library if successful, or nothing upon first failure.

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
echo "Started building native libraries... Directory: $DIR"
LIB_DIR="$DIR/lib"
mkdir -p $LIB_DIR
echo "Started '$0' $1 $2 $3 $4"

if [[ ! -z "$1" ]]; then
TARGET_BUILD_OS="$1"
fi

if [[ ! -z "$2" ]]; then
TARGET_BUILD_ARCH="$2"
fi

function set_target_build_os {
if [[ -z "$TARGET_BUILD_OS" || $TARGET_BUILD_OS == "host" ]]; then
uname_str="$(uname -a)"
case "${uname_str}" in
*Microsoft*) TARGET_BUILD_OS="windows";;
*microsoft*) TARGET_BUILD_OS="windows";;
Linux*) TARGET_BUILD_OS="linux";;
Darwin*) TARGET_BUILD_OS="macos";;
CYGWIN*) TARGET_BUILD_OS="linux";;
MINGW*) TARGET_BUILD_OS="windows";;
*Msys) TARGET_BUILD_OS="windows";;
*) TARGET_BUILD_OS="UNKNOWN:${uname_str}"
esac

if [[
"$TARGET_BUILD_OS" != "windows" &&
"$TARGET_BUILD_OS" != "macos" &&
"$TARGET_BUILD_OS" != "linux"
]]; then
echo "Unknown target build operating system: $TARGET_BUILD_OS"
exit 1
fi

echo "Target build operating system: '$TARGET_BUILD_OS' (host)"
else
if [[
"$TARGET_BUILD_OS" == "windows" ||
"$TARGET_BUILD_OS" == "macos" ||
"$TARGET_BUILD_OS" == "linux"
]]; then
echo "Target build operating system: '$TARGET_BUILD_OS' (override)"
else
echo "Unknown '$TARGET_BUILD_OS' passed as first argument. Use 'host' to use the host build platform or use either: 'windows', 'macos', 'linux'."
exit 1
fi
fi
}

function set_target_build_arch {
if [[ -z "$TARGET_BUILD_ARCH" || $TARGET_BUILD_ARCH == "default" ]]; then
if [[ "$TARGET_BUILD_OS" == "macos" ]]; then
TARGET_BUILD_ARCH="x86_64;arm64"
else
TARGET_BUILD_ARCH="$(uname -m)"
fi

echo "Target build CPU architecture: '$TARGET_BUILD_ARCH' (default)"
else
if [[ "$TARGET_BUILD_ARCH" == "x86_64" || "$TARGET_BUILD_ARCH" == "arm64" ]]; then
echo "Target build CPU architecture: '$TARGET_BUILD_ARCH' (override)"
else
echo "Unknown '$TARGET_BUILD_ARCH' passed as second argument. Use 'default' to use the host CPU architecture or use either: 'x86_64', 'arm64'."
exit 1
fi
fi
if [[ "$TARGET_BUILD_OS" == "macos" ]]; then
CMAKE_ARCH_ARGS="-DCMAKE_OSX_ARCHITECTURES=$TARGET_BUILD_ARCH"
fi
}

set_target_build_os
set_target_build_arch

function exit_if_last_command_failed() {
error=$?
if [ $error -ne 0 ]; then
echo "Last command failed: $error"
exit $error
fi
}

function build_library() {
echo "Building native library..."
BUILD_DIR="$DIR/cmake-build-release"
rm -rf BUILD_DIR

cmake -S $DIR/ext/flecs -B $BUILD_DIR $CMAKE_ARCH_ARGS \
`# change output directories` \
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=$BUILD_DIR -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$BUILD_DIR -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=$BUILD_DIR -DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=$BUILD_DIR \
`# project specific` \
-DFLECS_STATIC_LIBS=OFF
exit_if_last_command_failed
cmake --build $BUILD_DIR --config Release
exit_if_last_command_failed

if [[ "$TARGET_BUILD_OS" == "linux" ]]; then
LIBRARY_FILENAME="libflecs.so"
LIBRARY_FILE_PATH_BUILD="$(readlink -f $BUILD_DIR/$LIBRARY_FILENAME)"
elif [[ "$TARGET_BUILD_OS" == "macos" ]]; then
LIBRARY_FILENAME="libflecs.dylib"
LIBRARY_FILE_PATH_BUILD="$(perl -MCwd -e 'print Cwd::abs_path shift' $BUILD_DIR/$LIBRARY_FILENAME)"
elif [[ "$TARGET_BUILD_OS" == "windows" ]]; then
LIBRARY_FILENAME="flecs.dll"
LIBRARY_FILE_PATH_BUILD="$BUILD_DIR/$LIBRARY_FILENAME"
fi
LIBRARY_FILE_PATH="$LIB_DIR/$LIBRARY_FILENAME"

if [[ ! -f "$LIBRARY_FILE_PATH_BUILD" ]]; then
echo "The file '$LIBRARY_FILE_PATH_BUILD' does not exist!"
exit 1
fi

mv "$LIBRARY_FILE_PATH_BUILD" "$LIBRARY_FILE_PATH"
exit_if_last_command_failed
echo "Copied '$LIBRARY_FILE_PATH_BUILD' to '$LIBRARY_FILE_PATH'"

rm -r $BUILD_DIR
exit_if_last_command_failed
echo "Building native library finished!"
}

build_library
ls -d "$LIB_DIR"/*

echo "Finished '$0'!"
$DIR/ext/scripts/c/library/main.sh \
$DIR/src/c/production/flecs \
$DIR/lib \
"flecs" \
"flecs" \
"" \
"" \
26 changes: 26 additions & 0 deletions src/c/production/flecs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.16)

project(flecs C)
set(CMAKE_C_STANDARD 11)

get_filename_component(FLECS_DIRECTORY_PATH "../../../../ext/flecs" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
include(${FLECS_DIRECTORY_PATH}/cmake/target_default_compile_warnings.cmake)
include(${FLECS_DIRECTORY_PATH}/cmake/target_default_compile_options.cmake)
file(GLOB_RECURSE INC ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${FLECS_DIRECTORY_PATH}/include/*.h)
file(GLOB_RECURSE SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c ${FLECS_DIRECTORY_PATH}/src/*.c)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${FLECS_DIRECTORY_PATH}/include)

message("FILES: ${INC} ${SRC}")

add_library(flecs SHARED ${INC} ${SRC})

set_target_properties(flecs PROPERTIES LINKER_LANGUAGE C)

if(WIN32)
target_link_libraries(flecs wsock32 ws2_32)
endif()

#target_default_compile_options_c(flecs)
#target_default_compile_warnings_c(flecs)
#target_include_directories(flecs PUBLIC ${FLECS_DIRECTORY_PATH}/include ${CMAKE_CURRENT_SOURCE_DIR}/include)
6 changes: 6 additions & 0 deletions src/c/production/flecs/include/flecs_pinvoke.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "pinvoke.h"
#include "flecs.h"

PINVOKE_API_DECL ecs_id_t pinvoke_ECS_PAIR();
PINVOKE_API_DECL ecs_entity_t pinvoke_EcsOnUpdate();
PINVOKE_API_DECL ecs_entity_t pinvoke_EcsDependsOn();
95 changes: 95 additions & 0 deletions src/c/production/flecs/include/pinvoke.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Provides macros, types, and functions that make P/Invoke with C# easier.
#pragma once

#if defined(__APPLE__) && __has_include("TargetConditionals.h")
#include <TargetConditionals.h>

#define PINVOKE_TARGET_CPU_X64 TARGET_CPU_X86_64
#define PINVOKE_TARGET_CPU_X86 TARGET_CPU_X86
#define PINVOKE_TARGET_CPU_ARM64 TARGET_CPU_ARM64

#define PINVOKE_TARGET_OS_WINDOWS 0
#define PINVOKE_TARGET_OS_LINUX 0
#define PINVOKE_TARGET_OS_MACOS TARGET_OS_OSX
#define PINVOKE_TARGET_OS_IOS TARGET_OS_IOS

#define PINVOKE_TARGET_ENV_MSVC 0
#define PINVOKE_TARGET_ENV_GNU 0
#else
#define PINVOKE_TARGET_CPU_X64 defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
#define PINVOKE_TARGET_CPU_X86 defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
#define PINVOKE_TARGET_CPU_ARM64 defined(__aarch64__) || defined(_M_ARM64)

#define PINVOKE_TARGET_OS_WINDOWS defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#define PINVOKE_TARGET_OS_LINUX defined(__linux__)
#define PINVOKE_TARGET_OS_MACOS 0
#define PINVOKE_TARGET_OS_IOS 0

#define PINVOKE_TARGET_ENV_MSVC defined(_MSC_VER)
#define PINVOKE_TARGET_ENV_GNU defined(__GNUC__)
#endif

#if PINVOKE_TARGET_OS_WINDOWS && PINVOKE_TARGET_ENV_GNU
#if PINVOKE_TARGET_CPU_X64
#define PINVOKE_TARGET_NAME "x86_64-pc-windows-gnu"
#elif PINVOKE_TARGET_CPU_X86
#define PINVOKE_TARGET_NAME "i686-pc-windows-gnu"
#elif PINVOKE_TARGET_CPU_ARM64
#define PINVOKE_TARGET_NAME "aarch64-pc-windows-gnu"
#else
#error "Unknown computer architecture for Windows (GNU)."
#endif
#elif PINVOKE_TARGET_OS_WINDOWS && PINVOKE_TARGET_ENV_MSVC
#if PINVOKE_TARGET_CPU_X64
#define PINVOKE_TARGET_NAME "x86_64-pc-windows-msvc"
#elif PINVOKE_TARGET_CPU_X86
#define PINVOKE_TARGET_NAME "i686-pc-windows-msvc"
#elif PINVOKE_TARGET_CPU_ARM64
#define PINVOKE_TARGET_NAME "aarch64-pc-windows-msvc"
#else
#error "Unknown computer architecture for Windows (Microsoft Visual C++)."
#endif
#elif PINVOKE_TARGET_OS_LINUX
#if PINVOKE_TARGET_CPU_X64
#define PINVOKE_TARGET_NAME "x86_64-unknown-linux-gnu"
#elif PINVOKE_TARGET_CPU_X86
#define PINVOKE_TARGET_NAME "i686-unknown-linux-gnu"
#elif PINVOKE_TARGET_CPU_ARM64
#define PINVOKE_TARGET_NAME "aarch64-unknown-linux-gnu"
#else
#error "Unknown computer architecture for Linux."
#endif
#elif PINVOKE_TARGET_OS_MACOS
#if PINVOKE_TARGET_CPU_X64
#define PINVOKE_TARGET_NAME "x86_64-apple-darwin"
#elif PINVOKE_TARGET_CPU_X86
#define PINVOKE_TARGET_NAME "i686-apple-darwin"
#elif PINVOKE_TARGET_CPU_ARM64
#define PINVOKE_TARGET_NAME "aarch64-apple-darwin"
#else
#error "Unknown computer architecture for macOS."
#endif
#elif PINVOKE_TARGET_OS_IOS
#if PINVOKE_TARGET_CPU_X64
#define PINVOKE_TARGET_NAME "x86_64-apple-ios"
#elif PINVOKE_TARGET_CPU_X86
#define PINVOKE_TARGET_NAME "i686-apple-ios"
#elif PINVOKE_TARGET_CPU_ARM64
#define PINVOKE_TARGET_NAME "aarch64-apple-ios"
#else
#error "Unknown computer architecture for iOS."
#endif
#else
#define PINVOKE_TARGET_NAME 0
#endif

#if PINVOKE_TARGET_OS_WINDOWS
#define PINVOKE_API_DECL __declspec(dllexport)
#else
#define PINVOKE_API_DECL extern
#endif

PINVOKE_API_DECL const char* pinvoke_get_platform_name()
{
return PINVOKE_TARGET_NAME;
}
17 changes: 17 additions & 0 deletions src/c/production/flecs/src/flecs_pinvoke.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "pinvoke.h"
#include "flecs.h"

ecs_id_t pinvoke_ECS_PAIR()
{
return ECS_PAIR;
}

ecs_entity_t pinvoke_EcsOnUpdate()
{
return EcsOnUpdate;
}

ecs_entity_t pinvoke_EcsDependsOn()
{
return EcsDependsOn;
}
Loading