From 630e0104d54669ce321a2fc10ffe43e40cacf2eb Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Thu, 5 Nov 2020 15:42:00 +0700 Subject: [PATCH] Add support for Windows 64-bit Tested on NixOS, Ubuntu and Arch Linux. Not yet able to compile for Windows 32-bit on NixOS or on Windows itself. Part of #5 --- CHANGELOG.md | 5 +- README.md | 11 + addons/godot_xterm/native/SConstruct | 354 +++++++++++++----- addons/godot_xterm/native/build.sh | 36 +- addons/godot_xterm/native/dist/.gdignore | 0 .../x11/libgodotxtermnative.so.REMOVED.git-id | 1 - .../native/godotxtermnative.gdnlib | 12 +- addons/godot_xterm/native/shell.nix | 12 +- .../native/src/libgodotxtermnative.cpp | 5 +- docker-compose.yml | 2 +- 10 files changed, 328 insertions(+), 110 deletions(-) delete mode 100644 addons/godot_xterm/native/dist/.gdignore delete mode 100644 addons/godot_xterm/native/dist/x11/libgodotxtermnative.so.REMOVED.git-id diff --git a/CHANGELOG.md b/CHANGELOG.md index febdca5..8917ffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Support for Windows 64-bit. + ### Changed -- Updated build script (`addons/godot_xterm/native/build.sh`). Git submodules will now be initialized if they haven't already. Moved nix-shell related stuff to a seperate shell.nix file so the same build command can be used on all Linux based OSes. +- Updated build script. `./build.sh` will create a debug build of the gdnative library for the current platform. `./build.sh release-all` will create release build of the gdnative library for every supported platform. - Positioned background rect at 0,0 so it is no longer offset if a margin is added when Terminal is a child of a Container node. - Removed all pre-compiled binaries using BFG [Repo-Cleaner](https://rtyley.github.io/bfg-repo-cleaner/), thus re-writing git history. diff --git a/README.md b/README.md index 698b05c..43bf8ca 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,17 @@ Terminal emulator for Godot using GDNative and [libtsm](https://github.com/Aetf/ ![Screenshot of Main Menu Scene](./docs/screenshot.png) +## Supported Platforms + +### Confirmed: +- Linux 64-bit (primarily developed/tested on this platform) +- Windows 64-bit + +### Planned/untested: +- Linux 32-bit +- Windows 32-bit +- MacOS 64-bit + ## Building **Important**: It is recommended that you build the native binaries before opening this demo project, otherwise the Godot editor will automatically modify the example scenes when it can't find the native libs, such that they won't work when the files _are_ in place. diff --git a/addons/godot_xterm/native/SConstruct b/addons/godot_xterm/native/SConstruct index 582f04d..3c3e9b4 100644 --- a/addons/godot_xterm/native/SConstruct +++ b/addons/godot_xterm/native/SConstruct @@ -1,118 +1,294 @@ -#!python -import os, subprocess +#!/usr/bin/env python +""" +This file is modified version of the godot-cpp SConstruct file: https://github.com/godotengine/godot-cpp/blob/master/SConstruct +which is published under the following license: + +MIT License +Copyright (c) 2017-2020 GodotNativeTools +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +""" + +import os +import sys +import subprocess + +if sys.version_info < (3,): + def decode_utf8(x): + return x +else: + import codecs + def decode_utf8(x): + return codecs.utf_8_decode(x)[0] + +# Workaround for MinGW. See: +# http://www.scons.org/wiki/LongCmdLinesOnWin32 +if (os.name=="nt"): + import subprocess + + def mySubProcess(cmdline,env): + #print "SPAWNED : " + cmdline + startupinfo = subprocess.STARTUPINFO() + startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW + proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, startupinfo=startupinfo, shell = False, env = env) + data, err = proc.communicate() + rv = proc.wait() + if rv: + print("=====") + print(err.decode("utf-8")) + print("=====") + return rv + + def mySpawn(sh, escape, cmd, args, env): + + newargs = ' '.join(args[1:]) + cmdline = cmd + " " + newargs + + rv=0 + if len(cmdline) > 32000 and cmd.endswith("ar") : + cmdline = cmd + " " + args[1] + " " + args[2] + " " + for i in range(3,len(args)) : + rv = mySubProcess( cmdline + args[i], env ) + if rv : + break + else: + rv = mySubProcess( cmdline, env ) + + return rv + +def add_sources(sources, dir, extension): + for f in os.listdir(dir): + if f.endswith('.' + extension): + sources.append(dir + '/' + f) + + +# Try to detect the host platform automatically. +# This is used if no `platform` argument is passed. +if sys.platform.startswith('linux'): + host_platform = 'linux' +elif sys.platform == 'darwin': + host_platform = 'osx' +elif sys.platform == 'win32' or sys.platform == 'msys': + host_platform = 'windows' +else: + raise ValueError( + 'Could not detect platform automatically, please specify with ' + 'platform=' + ) + +VariantDir('./external/libtsm/build', './external/libtsm/src', duplicate=0) +env = Environment(ENV = os.environ) + +is64 = sys.maxsize > 2**32 +if ( + env['TARGET_ARCH'] == 'amd64' or + env['TARGET_ARCH'] == 'emt64' or + env['TARGET_ARCH'] == 'x86_64' or + env['TARGET_ARCH'] == 'arm64-v8a' +): + is64 = True opts = Variables([], ARGUMENTS) -# Gets the standard flags CC, CCX, etc. -env = DefaultEnvironment() +opts.Add(EnumVariable( + 'platform', + 'Target platform', + host_platform, + allowed_values=('linux', 'osx', 'windows'), + ignorecase=2 +)) +opts.Add(EnumVariable( + 'bits', + 'Target platform bits', + '64' if is64 else '32', + ('32', '64') +)) +opts.Add(EnumVariable( + 'target', + 'Compilation target', + 'debug', + allowed_values=('debug', 'release'), + ignorecase=2 +)) + +opts.Update(env) +Help(opts.GenerateHelpText(env)) # Add PATH to environment so scons can find commands such as g++, etc. env.AppendENVPath('PATH', os.getenv('PATH')) -# Define our options -opts.Add(EnumVariable('target', "Compilation target", 'debug', ['d', 'debug', 'r', 'release'])) -opts.Add(EnumVariable('platform', "Compilation platform", '', ['', 'windows', 'x11', 'linux', 'osx'])) -opts.Add(EnumVariable('p', "Compilation target, alias for 'platform'", '', ['', 'windows', 'x11', 'linux', 'osx'])) -opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", 'no')) -opts.Add(PathVariable('target_path', 'The path where the lib is installed.', 'bin/')) -opts.Add(PathVariable('target_name', 'The library name.', 'libgodotxtermnative', PathVariable.PathAccept)) +# Generate godot-cpp bindings. +# Options such as platform, target, etc. will be forwarded to the godot-cpp SConscript. +ARGUMENTS['generate_bindings'] = True +SConscript('external/godot-cpp/SConstruct') -# Local dependency paths, adapt them to your setup -godot_headers_path = "external/godot-cpp/godot_headers/" -cpp_bindings_path = "external/godot-cpp/" -cpp_library = "libgodot-cpp" -libtsm_path = "external/libtsm/" -# only support 64 at this time.. -bits = 64 +# Compile for Linux. +if env['platform'] == 'linux': + env.Append(CPPDEFINES=['PLATFORM_LINUX']) -# Updates the environment with the option variables. -opts.Update(env) - -# Process some arguments -if env['use_llvm']: - env['CC'] = 'clang' - env['CXX'] = 'clang++' -else: env['CC'] = 'gcc' env['CXX'] = 'g++' -if env['p'] != '': - env['platform'] = env['p'] + env.Append(CCFLAGS=['-fPIC', '-Wwrite-strings']) + env.Append(LINKFLAGS=["-Wl,-R'$$ORIGIN'"]) -if env['platform'] == '': - print("No valid target platform selected.") - quit(); + if env['target'] == 'debug': + env.Append(CCFLAGS=['-Og', '-g']) + elif env['target'] == 'release': + env.Append(CCFLAGS=['-O3']) -# For the reference: -# - CCFLAGS are compilation flags shared between C and C++ -# - CFLAGS are for C-specific compilation flags -# - CXXFLAGS are for C++-specific compilation flags -# - CPPFLAGS are for pre-processor flags -# - CPPDEFINES are for pre-processor defines -# - LINKFLAGS are for linking flags + if env['bits'] == '64': + env.Append(CCFLAGS=['-m64']) + env.Append(LINKFLAGS=['-m64']) + elif env['bits'] == '32': + env.Append(CCFLAGS=['-m32']) + env.Append(LINKFLAGS=['-m32']) + + +# Compile for OSX. +elif env['platform'] == 'osx': + env.Append(CPPDEFINES=['PLATFORM_OSX']) + + env['CC'] = 'clang' + env['CXX'] = 'clang++' + + if env['bits'] == '32': + raise ValueError( + 'Only 64-bit builds are supported for the macOS target.' + ) -# Check our platform specifics -if env['platform'] == "osx": - env['target_path'] += 'osx/' - cpp_library += '.osx' env.Append(CCFLAGS=['-arch', 'x86_64']) - env.Append(CXXFLAGS=['-std=c++17']) - env.Append(LINKFLAGS=['-arch', 'x86_64']) - if env['target'] in ('debug', 'd'): - env.Append(CCFLAGS=['-g', '-O2']) - else: - env.Append(CCFLAGS=['-g', '-O3']) + env.Append(LINKFLAGS=[ + '-arch', + 'x86_64', + '-Wl,-undefined,dynamic_lookup', + ]) -elif env['platform'] in ('x11', 'linux'): - env['target_path'] += 'x11/' - cpp_library += '.linux' - env.Append(CCFLAGS=['-fPIC', '-shared']) - env.Append(CXXFLAGS=['-std=c++17', '-shared', '-pthread']) - if env['target'] in ('debug', 'd'): - env.Append(CCFLAGS=['-g3', '-Og']) - else: - env.Append(CCFLAGS=['-g', '-O3']) + if env['target'] == 'debug': + env.Append(CCFLAGS=['-Og', '-g']) + elif env['target'] == 'release': + env.Append(CCFLAGS=['-O3']) -elif env['platform'] == "windows": - env['target_path'] += 'win64/' - cpp_library += '.windows' - # This makes sure to keep the session environment variables on windows, - # that way you can run scons in a vs 2017 prompt and it will find all the required tools - env.Append(ENV=os.environ) - env.Append(CPPDEFINES=['WIN32', '_WIN32', '_WINDOWS', '_CRT_SECURE_NO_WARNINGS']) - env.Append(CCFLAGS=['-W3', '-GR']) - if env['target'] in ('debug', 'd'): - env.Append(CPPDEFINES=['_DEBUG']) - env.Append(CCFLAGS=['-EHsc', '-MDd', '-ZI']) - env.Append(LINKFLAGS=['-DEBUG']) - else: - env.Append(CPPDEFINES=['NDEBUG']) - env.Append(CCFLAGS=['-O2', '-EHsc', '-MD']) +# Compile for Windows. +elif env['platform'] == 'windows': + env.Append(CPPDEFINES=['PLATFORM_WINDOWS']) -if env['target'] in ('debug', 'd'): - cpp_library += '.debug' -else: - cpp_library += '.release' + env.Append(CCFLAGS=['-Wwrite-strings']) + env.Append(LINKFLAGS=[ + '--static', + '-Wl,--no-undefined', + '-static-libgcc', + '-static-libstdc++', + ]) -cpp_library += '.' + str(bits) + if env['target'] == 'debug': + env.Append(CCFLAGS=['-Og', '-g']) + elif env['target'] == 'release': + env.Append(CCFLAGS=['-O3']) -# make sure our binding library is properly includes -env.Append(CPPPATH=['.', godot_headers_path, cpp_bindings_path + 'include/', cpp_bindings_path + 'include/core/', cpp_bindings_path + 'include/gen/', libtsm_path + 'src/tsm', libtsm_path + 'external']) -env.Append(LIBPATH=[cpp_bindings_path + 'bin/', libtsm_path + 'build/src/tsm']) -env.Append(LIBS=[cpp_library, 'tsm', 'util']) # Note util used by pseudoterminal, tsm used by terminal. + # On Windows. + if host_platform == 'windows': + env = env.Clone(tools=['mingw']) + env["SPAWN"] = mySpawn + + # On Linux or MacOS. + elif host_platform == 'linux' or host_platform == 'osx': + if env['bits'] == '64': + env['CC'] = 'x86_64-w64-mingw32-gcc' + env['CXX'] = 'x86_64-w64-mingw32-g++' + env['AR'] = "x86_64-w64-mingw32-ar" + env['RANLIB'] = "x86_64-w64-mingw32-ranlib" + env['LINK'] = "x86_64-w64-mingw32-g++" + elif env['bits'] == '32': + env['CC'] = 'i686-w64-mingw32-gcc' + env['CXX'] = 'i686-w64-mingw32-g++' + env['AR'] = "i686-w64-mingw32-ar" + env['RANLIB'] = "i686-w64-mingw32-ranlib" + env['LINK'] = "i686-w64-mingw32-g++" + + +# Build libtsm as a static library. +Execute([ + Delete('external/libtsm/build/src'), + Delete('external/libtsm/build/external'), + Copy('external/libtsm/build/src', 'external/libtsm/src'), + Copy('external/libtsm/build/external', 'external/libtsm/external'), +]) + +env.Append(CPPPATH=[ + 'external/libtsm/src/shared', + 'external/libtsm/external', +]) -# tweak this if you want to use different folders, or more folders, to store your source code in. -env.Append(CPPPATH=['src/']) sources = [] -sources.append(Glob('src/*.c')) -sources.append(Glob('src/*.cpp')) +sources.append('external/libtsm/build/src/shared/shl-htable.c') +sources.append(Glob('external/libtsm/build/src/tsm/*.c')) +sources.append(Glob('external/libtsm/build/external/wcwidth/*.c')) + +libtsm = env.StaticLibrary( + target='external/libtsm/build/bin/libtsm.{}.{}.{}{}'.format( + env['platform'], + env['target'], + env['bits'], + env['LIBSUFFIX'] + ), source=sources +) +Default(libtsm) -library = env.SharedLibrary(target=env['target_path'] + env['target_name'] , source=sources) +# Build libgodot-xterm. +#env.Append(CCFLAGS=['-std=c++14']) +env.Append(CPPPATH=[ + 'src/', + 'external/libtsm/build/src/tsm', + 'external/godot-cpp/include/', + 'external/godot-cpp/include/core/', + 'external/godot-cpp/include/gen/', + 'external/godot-cpp/godot_headers/' +]) +env.Append(LIBPATH=[ + 'external/godot-cpp/bin/', + 'external/libtsm/build/bin/', +]) +env.Append(LIBS=[ + 'libgodot-cpp.{}.{}.{}'.format( + env['platform'], + env['target'], + env['bits'] + ), + 'libtsm.{}.{}.{}'.format( + env['platform'], + env['target'], + env['bits'] + ), +]) + +sources = [] +sources.append('src/libgodotxtermnative.cpp') +sources.append('src/terminal.cpp') + +# Psuedoterminal not supported on windows yet. +if env['platform'] != 'windows': + sources.append('src/pseudoterminal.cpp') + env.Append(LIBS=['util']) + +if env['platform'] == 'linux': + suffix = "so" +elif env['platform'] == 'windows': + suffix = "dll" +elif env['platform'] == 'osx': + suffix = "dylib" + +library = env.SharedLibrary( + target='bin/libgodot-xterm.{}.{}.{}'.format( + env['platform'], + env['bits'], + suffix, + ), source=sources +) Default(library) - -# Generates help for the -h scons option. -Help(opts.GenerateHelpText(env)) diff --git a/addons/godot_xterm/native/build.sh b/addons/godot_xterm/native/build.sh index baf1c4a..f019bac 100755 --- a/addons/godot_xterm/native/build.sh +++ b/addons/godot_xterm/native/build.sh @@ -1,37 +1,47 @@ #!/bin/sh set -e + # Get the absolute path to the directory this script is in. NATIVE_DIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + # Run script inside a nix shell if it is available. if command -v nix-shell && [ $NIX_PATH ] && [ -z $IN_NIX_SHELL ]; then cd ${NATIVE_DIR} - nix-shell shell.nix --pure --run "./build.sh" + nix-shell --pure --command "./build.sh $1" exit fi -# Build libtsm. + +# Update git submodules. LIBTSM_DIR=${NATIVE_DIR}/external/libtsm if [ ! -d "$LIBTSM_DIR" ]; then cd ${NATIVE_DIR} git submodule update --init --recursive -- $LIBTSM_DIR fi -cd $LIBTSM_DIR -mkdir -p build -cd build -cmake -DBUILD_SHARED_LIBS=n .. -make - -# Build godot-cpp. GODOT_CPP_DIR=${NATIVE_DIR}/external/godot-cpp if [ ! -d "${GODOT_CPP_DIR}" ]; then cd ${NATIVE_DIR} git submodule update --init --recursive -- $GODOT_CPP_DIR fi -cd $GODOT_CPP_DIR -scons platform=linux generate_bindings=yes -j12 -# Build godotxtermnative. + +# Build libgodot-xterm. cd ${NATIVE_DIR} -scons platform=linux +case $1 in + "release-all") + # Cross-compile release build for all platforms. + scons platform=linux bits=64 target=release -j$(nproc) + if [ $IN_NIX_SHELL ]; then + nix-shell --pure --argstr system i686-linux --run 'scons platform=linux bits=32 target=release -j$(nproc)' + fi + scons platform=windows bits=64 target=release -j$(nproc) + #scons platform=windows bits=32 target=release -j$(nproc) + #scons platform=osx bits=64 target=release -j$(nproc) + ;; + *) + # Default: Compile debug build for the current platform. + scons -j$(nproc) + ;; +esac diff --git a/addons/godot_xterm/native/dist/.gdignore b/addons/godot_xterm/native/dist/.gdignore deleted file mode 100644 index e69de29..0000000 diff --git a/addons/godot_xterm/native/dist/x11/libgodotxtermnative.so.REMOVED.git-id b/addons/godot_xterm/native/dist/x11/libgodotxtermnative.so.REMOVED.git-id deleted file mode 100644 index cedc0a4..0000000 --- a/addons/godot_xterm/native/dist/x11/libgodotxtermnative.so.REMOVED.git-id +++ /dev/null @@ -1 +0,0 @@ -fc0f54c9d43467a3d3e389c4a656f4fa8431a3a4 \ No newline at end of file diff --git a/addons/godot_xterm/native/godotxtermnative.gdnlib b/addons/godot_xterm/native/godotxtermnative.gdnlib index 73b7bd4..c339eed 100644 --- a/addons/godot_xterm/native/godotxtermnative.gdnlib +++ b/addons/godot_xterm/native/godotxtermnative.gdnlib @@ -7,10 +7,16 @@ reloadable=true [entry] -X11.64="res://addons/godot_xterm/native/bin/x11/libgodotxtermnative.so" -Server.64="res://addons/godot_xterm/native/bin/x11/libgodotxtermnative.so" +Server.64="res://addons/godot_xterm/native/bin/libgodot-xterm.linux.64.so" +X11.64="res://addons/godot_xterm/native/bin/libgodot-xterm.linux.64.so" +X11.32="res://addons/godot_xterm/native/bin/libgodot-xterm.linux.32.so" +Windows.64="res://addons/godot_xterm/native/bin/libgodot-xterm.windows.64.dll" +OSX.64="res://addons/godot_xterm/native/bin/libgodot-xterm.osx.64.dylib" [dependencies] -X11.64=[ ] Server.64=[ ] +X11.64=[ ] +X11.32=[ ] +Windows.64=[ ] +OSX.64=[ ] diff --git a/addons/godot_xterm/native/shell.nix b/addons/godot_xterm/native/shell.nix index abcd05d..ef605ae 100644 --- a/addons/godot_xterm/native/shell.nix +++ b/addons/godot_xterm/native/shell.nix @@ -1,11 +1,21 @@ -with (import {}); +# Use --argstr system i686-linux to build for 32-bit linux. +{ system ? builtins.currentSystem }: +with (import { + inherit system; +}); mkShell { buildInputs = with pkgs; [ binutils.bintools cmake git libxkbcommon + nix pkg-config scons + ] ++ lib.optionals (system == builtins.currentSystem) [ + # Additional dependencies for cross-compiling for Windows and OSX. + clang + pkgsCross.mingwW64.buildPackages.gcc + pkgsCross.mingw32.buildPackages.gcc ]; } diff --git a/addons/godot_xterm/native/src/libgodotxtermnative.cpp b/addons/godot_xterm/native/src/libgodotxtermnative.cpp index 567c43d..9d62953 100644 --- a/addons/godot_xterm/native/src/libgodotxtermnative.cpp +++ b/addons/godot_xterm/native/src/libgodotxtermnative.cpp @@ -1,5 +1,7 @@ #include "terminal.h" +#if defined(PLATFORM_LINUX) || defined(PLATFORM_OSX) #include "pseudoterminal.h" +#endif extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) { @@ -16,6 +18,7 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) godot::Godot::nativescript_init(handle); godot::register_tool_class(); - +#if defined(PLATFORM_LINUX) || defined(PLATFORM_OSX) godot::register_class(); +#endif } diff --git a/docker-compose.yml b/docker-compose.yml index ce0fc40..1c387de 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ services: build: context: . dockerfile: ./dockerfiles/nixos - command: /src/addons/godot_xterm/native/build.sh + command: /src/addons/godot_xterm/native/build.sh release-all build-ubuntu: build: context: .