From 55ce1da2dc277dc989a7265641d7c1c560d77dd1 Mon Sep 17 00:00:00 2001 From: QuentinCaffeino Date: Thu, 22 Feb 2018 23:16:25 +0100 Subject: [PATCH 1/4] Added crosscompiling for linux-windows; Added makefile --- Makefile | 48 +++++++++++++++++++++++++++ SConstruct | 97 +++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 115 insertions(+), 30 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..a3285edc --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ + +GODOT_BIN_PATH = ../godot_fork/bin/godot.x11.tools.64.llvm +HEADERS = ../godot_headers +TARGET = debug +NAME = godot-cpp + +BASE = scons n=$(NAME) generate_bindings=yes target=$(TARGET) headers=$(HEADERS) godotbinpath=godot -j4 +LINUX = $(BASE) p=linux +WINDOWS = $(BASE) p=windows +OSX = $(BASE) p=osx + + +all: + make linux + make windows + + +linux: + make linux32 + make linux64 + +linux32: SConstruct + $(LINUX) a=32 + +linux64: SConstruct + $(LINUX) a=64 + + +windows: + make windows32 + make windows64 + +windows32: SConstruct + $(WINDOWS) a=32 + +windows64: SConstruct + $(WINDOWS) a=64 + + +osx: + make osx32 + make osx64 + +osx32: SConstruct + $(OSX) a=32 + +osx64: SConstruct + $(OSX) a=64 diff --git a/SConstruct b/SConstruct index 50a63cdf..1c36cc09 100644 --- a/SConstruct +++ b/SConstruct @@ -1,48 +1,83 @@ #!python -import os, subprocess + +import os, subprocess, platform +def add_source(sources, name): + sources.append(name) + +def add_sources(sources, dir, extension): + for f in os.listdir(dir): + if f.endswith('.' + extension): + sources.append(dir + '/' + f) + + +env = Environment() +host_platform = platform.system() +target_platform = ARGUMENTS.get('p', ARGUMENTS.get('platform', 'linux')) +target_arch = ARGUMENTS.get('a', ARGUMENTS.get('arch', '64')) +# default to debug build, must be same setting as used for cpp_bindings +target = ARGUMENTS.get('target', 'debug') # Local dependency paths, adapt them to your setup -godot_headers_path = ARGUMENTS.get("headers", os.getenv("GODOT_HEADERS", "../godot_headers/")) -godot_bin_path = ARGUMENTS.get("godotbinpath", os.getenv("GODOT_BIN_PATH", "../godot_fork/bin/godot.x11.tools.64.llvm")) +godot_headers = ARGUMENTS.get('headers', '../godot_headers') +godot_bin_path = ARGUMENTS.get('godotbinpath', os.getenv('GODOT_BIN_PATH', '../godot_fork/bin/godot.x11.tools.64.llvm')) +result_path = 'bin/' +result_name = ARGUMENTS.get('n', ARGUMENTS.get('name', os.path.relpath('.', '..'))) -target = ARGUMENTS.get("target", "debug") -platform = ARGUMENTS.get("p", ARGUMENTS.get("platform", "linux")) # 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 = Environment() -if platform == "windows": +if target_platform == 'windows': env = Environment(ENV = os.environ) -if ARGUMENTS.get("use_llvm", "no") == "yes": - env["CXX"] = "clang++" - -def add_sources(sources, directory): - for file in os.listdir(directory): - if file.endswith('.cpp'): - sources.append(directory + '/' + file) +if ARGUMENTS.get('use_llvm', 'no') == 'yes': + env['CXX'] = 'clang++' -if platform == "osx": - env.Append(CCFLAGS = ['-g','-O3', '-std=c++14', '-arch', 'x86_64']) - env.Append(LINKFLAGS = ['-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup']) +if target_platform == 'linux': + result_name += '.linux.' + target_arch -if platform == "linux": - env.Append(CCFLAGS = ['-fPIC', '-g','-O3', '-std=c++14']) + env['CXX']='gcc-5' + env.Append(CCFLAGS = [ '-fPIC', '-g', '-O3', '-std=c++14', '-Wwrite-strings' ]) + env.Append(LINKFLAGS = [ '-Wl,-R,\'$$ORIGIN\'' ]) -env.Append(CPPPATH=['.', godot_headers_path, 'include', 'include/core']) + if target_arch == '32': + env.Append(CCFLAGS = [ '-m32' ]) + env.Append(LINKFLAGS = [ '-m32' ]) + elif target_arch == '64': + env.Append(CCFLAGS = [ '-m64' ]) + env.Append(LINKFLAGS = [ '-m64' ]) -if platform == "windows": - if target == "debug": - env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '/MDd']) +elif target_platform == 'windows': + result_name += '.windows.' + target_arch + + if host_platform == 'Windows': + result_name += '.dll' + + env.Append(LINKFLAGS = [ '/WX' ]) + if target == 'debug': + env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '/MDd' ]) + else: + env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '/MD' ]) else: - env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '/MD']) + if target_arch == '32': + env['CXX']='i686-w64-mingw32-g++' + elif target_arch == '64': + env['CXX']='x86_64-w64-mingw32-g++' -sources = [] -add_sources(sources, "src/core") + env.Append(CCFLAGS = [ '-g', '-O3', '-std=c++14', '-Wwrite-strings' ]) + env.Append(LINKFLAGS = [ '--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++' ]) -if ARGUMENTS.get("generate_bindings", "no") == "yes": +elif platform == 'osx': + env.Append(CCFLAGS = [ '-g','-O3', '-std=c++14', '-arch', 'x86_64' ]) + env.Append(LINKFLAGS = [ '-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup' ]) + + +env.Append(CPPPATH=['.', godot_headers, 'include', 'include/core']) + + +if ARGUMENTS.get('generate_bindings', 'no') == 'yes': # TODO Generating the API should be done only if the Godot build is more recent than the JSON file json_api_file = os.path.join(os.getcwd(), 'godot_api.json') @@ -52,11 +87,13 @@ if ARGUMENTS.get("generate_bindings", "no") == "yes": import binding_generator - binding_generator.generate_bindings(json_api_file) -add_sources(sources, "src") -library = env.StaticLibrary(target='bin/godot_cpp_bindings', source=sources) +sources = [] +add_sources(sources, 'src/core', 'cpp') +add_sources(sources, 'src', 'cpp') + + +library = env.StaticLibrary(target=result_path + '/' + result_name, source=sources) Default(library) - From 38cfd627f394862bf09ce9c44bc270f3b230ad98 Mon Sep 17 00:00:00 2001 From: QuentinCaffeino Date: Thu, 22 Feb 2018 23:20:25 +0100 Subject: [PATCH 2/4] Fixed godo bin path in makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a3285edc..976f0022 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ HEADERS = ../godot_headers TARGET = debug NAME = godot-cpp -BASE = scons n=$(NAME) generate_bindings=yes target=$(TARGET) headers=$(HEADERS) godotbinpath=godot -j4 +BASE = scons n=$(NAME) generate_bindings=yes target=$(TARGET) headers=$(HEADERS) godotbinpath=$(GODOT_BIN_PATH) -j4 LINUX = $(BASE) p=linux WINDOWS = $(BASE) p=windows OSX = $(BASE) p=osx From 43f57c7f80a3014fa8d9491420ca33ff93968625 Mon Sep 17 00:00:00 2001 From: QuentinCaffeino Date: Sun, 25 Feb 2018 16:25:42 +0100 Subject: [PATCH 3/4] Bindings are generated by default; Replaced generate with regenerate flag; Updated readme --- Makefile | 3 ++- README.md | 57 +++++++++++++++++++++++------------------------------- SConstruct | 14 +++++--------- 3 files changed, 31 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 976f0022..95989ff9 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ GODOT_BIN_PATH = ../godot_fork/bin/godot.x11.tools.64.llvm +REGENERATE_BINDINGS = no HEADERS = ../godot_headers TARGET = debug NAME = godot-cpp -BASE = scons n=$(NAME) generate_bindings=yes target=$(TARGET) headers=$(HEADERS) godotbinpath=$(GODOT_BIN_PATH) -j4 +BASE = scons n=$(NAME) regenerate_bindings=$(REGENERATE_BINDINGS) target=$(TARGET) headers=$(HEADERS) godotbinpath=$(GODOT_BIN_PATH) -j4 LINUX = $(BASE) p=linux WINDOWS = $(BASE) p=windows OSX = $(BASE) p=osx diff --git a/README.md b/README.md index 918c39f8..4766a8ed 100644 --- a/README.md +++ b/README.md @@ -1,54 +1,45 @@ -# cpp_bindings +# godot-cpp C++ bindings for the Godot script API # Creating a GDNative library (Linux) Create a directory named `SimpleLibrary` with subdirectories `lib, src` -Getting latest `cpp_bindings` and `godot_headers` +Getting latest `godot-cpp` and `godot_headers` ``` -$ cd SimpleLibrary -$ git clone https://github.com/GodotNativeTools/cpp_bindings +$ git clone https://github.com/GodotNativeTools/godot-cpp $ git clone https://github.com/GodotNativeTools/godot_headers ``` -right now our directory structure should look like this +right now our directory structure should look like this: ``` -[SimpleLibrary] - ├── cpp_bindings/ - ├── godot_headers/ - ├── lib/ - └── src/ +godot-cpp +godot_headers +SimpleLibrary +├── lib/ +└── src/ ``` Now to generate cpp bindings ``` -$ cd cpp_bindings -``` - -Edit `SConstruct` file and assign your godot executable path at line:7 `godot_bin_path = "../godot_fork/bin/"`, - -Building cpp_bindings -``` -$ scons godotbinpath="../godot_fork/bin/godot_binary" headers="../godot_headers/" p=linux generate_bindings=yes +$ cd godot-cpp +$ scons godotbinpath="../godot_fork/bin/godot_binary" p=linux +$ cd .. ``` resulting libraries will be placed under `bin/` and the generated headers will be placed under `include/*` **Note:** -> `generate_bindings=yes` is used to generate C++ bindings (`godot_api.json` - Godot API) +> `regenerate_bindings=yes` is used to force regenerating C++ bindings (`godot_api.json` - Godot API) + > Include `use_llvm=yes` for using clang++ -Copy binding libraries into the `SimpleLibrary/lib` folder -``` -$ cd .. -$ cp cpp_bindings/bin/libgodot_cpp_bindings.a lib/ -``` + And our directory structure will be ``` -[SimpleLibrary] - ├── cpp_bindings/ - ├── godot_headers/ - ├── lib/ - │ └──libgodot_cpp_bindings.a - └── src/ +godot-cpp +└── bin/libgodot-cpp.a +godot_headers +SimpleLibrary +├── lib/ +└── src/ ``` # Creating simple class @@ -115,9 +106,9 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) # Compiling ``` -$ cd .. -$ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -Icpp_bindings/include -Igodot_headers -$ clang -o lib/libtest.so -shared src/init.os -Llib -lgodot_cpp_bindings +$ cd SimpleLibrary +$ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -I../godot-cpp/include -Igodot_headers +$ clang -o lib/libtest.so -shared src/init.os -L../godot-cpp/lib -lgodot-cpp ``` This creates the file `libtest.so` in your `SimpleLibrary/lib` directory. For windows you need to find out what compiler flags need to be used. diff --git a/SConstruct b/SConstruct index 1c36cc09..2171328e 100644 --- a/SConstruct +++ b/SConstruct @@ -3,9 +3,6 @@ import os, subprocess, platform -def add_source(sources, name): - sources.append(name) - def add_sources(sources, dir, extension): for f in os.listdir(dir): if f.endswith('.' + extension): @@ -21,7 +18,7 @@ target = ARGUMENTS.get('target', 'debug') # Local dependency paths, adapt them to your setup godot_headers = ARGUMENTS.get('headers', '../godot_headers') godot_bin_path = ARGUMENTS.get('godotbinpath', os.getenv('GODOT_BIN_PATH', '../godot_fork/bin/godot.x11.tools.64.llvm')) -result_path = 'bin/' +result_path = 'bin' result_name = ARGUMENTS.get('n', ARGUMENTS.get('name', os.path.relpath('.', '..'))) @@ -53,7 +50,7 @@ elif target_platform == 'windows': result_name += '.windows.' + target_arch if host_platform == 'Windows': - result_name += '.dll' + result_name += '.lib' env.Append(LINKFLAGS = [ '/WX' ]) if target == 'debug': @@ -77,10 +74,9 @@ elif platform == 'osx': env.Append(CPPPATH=['.', godot_headers, 'include', 'include/core']) -if ARGUMENTS.get('generate_bindings', 'no') == 'yes': - # TODO Generating the API should be done only if the Godot build is more recent than the JSON file - json_api_file = os.path.join(os.getcwd(), 'godot_api.json') - +# Generate bindings +json_api_file = os.path.join(os.getcwd(), 'godot_api.json') +if os.path.exists(json_api_file) == False or ARGUMENTS.get('regenerate_bindings', 'no') == 'yes': subprocess.call([os.path.expanduser(godot_bin_path), '--gdnative-generate-json-api', json_api_file]) # actually create the bindings here From af2861faf7073df6da34a0a272d96dde427b770d Mon Sep 17 00:00:00 2001 From: QuentinCaffeino Date: Sun, 25 Feb 2018 17:03:55 +0100 Subject: [PATCH 4/4] use_llvm fix --- SConstruct | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/SConstruct b/SConstruct index 2171328e..b251d432 100644 --- a/SConstruct +++ b/SConstruct @@ -28,14 +28,14 @@ env = Environment() if target_platform == 'windows': env = Environment(ENV = os.environ) -if ARGUMENTS.get('use_llvm', 'no') == 'yes': - env['CXX'] = 'clang++' - if target_platform == 'linux': result_name += '.linux.' + target_arch env['CXX']='gcc-5' + if ARGUMENTS.get('use_llvm', 'no') == 'yes': + env['CXX'] = 'clang++' + env.Append(CCFLAGS = [ '-fPIC', '-g', '-O3', '-std=c++14', '-Wwrite-strings' ]) env.Append(LINKFLAGS = [ '-Wl,-R,\'$$ORIGIN\'' ]) @@ -67,6 +67,9 @@ elif target_platform == 'windows': env.Append(LINKFLAGS = [ '--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++' ]) elif platform == 'osx': + if ARGUMENTS.get('use_llvm', 'no') == 'yes': + env['CXX'] = 'clang++' + env.Append(CCFLAGS = [ '-g','-O3', '-std=c++14', '-arch', 'x86_64' ]) env.Append(LINKFLAGS = [ '-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup' ])