Proofread SCons examples

This commit is contained in:
Rémi Verschelde
2019-07-03 09:46:30 +02:00
parent 6bef6354f5
commit 4beac25f03
3 changed files with 67 additions and 48 deletions

View File

@@ -22,10 +22,11 @@ Next, you will create a header file with a simple TTS class:
.. code:: cpp
/* tts.h */
#ifndef GODOT_TTS_H
#define GODOT_TTS_H
#include <reference.h>
#include "core/reference.h"
class TTS : public Reference {
GDCLASS(TTS, Reference);
@@ -34,7 +35,7 @@ Next, you will create a header file with a simple TTS class:
static void _bind_methods();
public:
bool say_text(String txt);
bool say_text(String p_txt);
TTS();
};
@@ -48,12 +49,13 @@ And then you'll add the cpp file.
/* tts.cpp */
#include "tts.h"
#include "festival/src/include/festival.h"
bool TTS::say_text(String txt) {
#include <festival.h>
bool TTS::say_text(String p_txt) {
//convert Godot String to Godot CharString to C string
return festival_say_text(txt.ascii().get_data());
return festival_say_text(p_txt.ascii().get_data());
}
void TTS::_bind_methods() {
@@ -89,8 +91,7 @@ With the following contents:
#include "register_types.h"
#include "class_db.h"
#include "core/class_db.h"
#include "tts.h"
void register_tts_types() {
@@ -98,7 +99,7 @@ With the following contents:
}
void unregister_tts_types() {
//nothing to do here
// Nothing to do here in this example.
}
Next, you need to create a ``SCsub`` file so the build system compiles
@@ -107,10 +108,11 @@ this module:
.. code:: python
# SCsub
Import('env')
env_tts = env
env_tts.add_source_files(env.modules_sources,"*.cpp") # Add all cpp files to the build
env_tts = env.Clone()
env_tts.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build
You'll need to install the external library on your machine to get the .a library files. See the library's official
documentation for specific instructions on how to do this for your operation system. We've included the
@@ -157,7 +159,7 @@ environment's paths:
.. code:: python
env_tts.Append(CPPPATH="speech_tools/include", "festival/src/include") # this is a path relative to /modules/tts/
env_tts.Append(CPPPATH=["speech_tools/include", "festival/src/include"]) # this is a path relative to /modules/tts/
# http://www.cstr.ed.ac.uk/projects/festival/manual/festival_28.html#SEC132 <-- Festival library documentation
env_tts.Append(LIBPATH=['libpath']) # this is a path relative to /modules/tts/ where your .a library files reside
# You should check with the documentation of the external library to see which library files should be included/linked
@@ -170,11 +172,13 @@ Example `SCsub` with custom flags:
.. code:: python
# SCsub
Import('env')
env_tts = env
env_tts.add_source_files(env.modules_sources,"*.cpp")
env_tts.Append(CXXFLAGS=['-O2', '-std=c++11'])
env_tts = env.Clone()
env_tts.add_source_files(env.modules_sources, "*.cpp")
env_tts.Append(CCFLAGS=['-O2']) # Flags for C and C++ code
env_tts.Append(CXXFLAGS=['-std=c++11']) # Flags for C++ code only
The final module should look like this:
@@ -201,7 +205,7 @@ You can now use your newly created module from any script:
::
var t = TTS.new()
var script = "Hello world. This is a test!"
var script = "Hello world. This is a test!"
var is_spoken = t.say_text(script)
print('is_spoken: ', is_spoken)

View File

@@ -57,6 +57,7 @@ Inside we will create a simple summator class:
.. code:: cpp
/* summator.h */
#ifndef SUMMATOR_H
#define SUMMATOR_H
@@ -71,14 +72,14 @@ Inside we will create a simple summator class:
static void _bind_methods();
public:
void add(int value);
void add(int p_value);
void reset();
int get_total() const;
Summator();
};
#endif
#endif // SUMMATOR_H
And then the cpp file.
@@ -88,23 +89,19 @@ And then the cpp file.
#include "summator.h"
void Summator::add(int value) {
count += value;
void Summator::add(int p_value) {
count += p_value;
}
void Summator::reset() {
count = 0;
}
int Summator::get_total() const {
return count;
}
void Summator::_bind_methods() {
ClassDB::bind_method(D_METHOD("add", "value"), &Summator::add);
ClassDB::bind_method(D_METHOD("reset"), &Summator::reset);
ClassDB::bind_method(D_METHOD("get_total"), &Summator::get_total);
@@ -142,12 +139,11 @@ With the following contents:
#include "summator.h"
void register_summator_types() {
ClassDB::register_class<Summator>();
ClassDB::register_class<Summator>();
}
void unregister_summator_types() {
//nothing to do here
// Nothing to do here in this example.
}
Next, we need to create a ``SCsub`` file so the build system compiles
@@ -156,9 +152,10 @@ this module:
.. code:: python
# SCsub
Import('env')
env.add_source_files(env.modules_sources,"*.cpp") # Add all cpp files to the build
env.add_source_files(env.modules_sources, "*.cpp") # Add all cpp files to the build
With multiple sources, you can also add each file individually to a Python
string list:
@@ -177,8 +174,8 @@ environment's paths:
.. code:: python
env.Append(CPPPATH="mylib/include") # this is a relative path
env.Append(CPPPATH="#myotherlib/include") # this is an 'absolute' path
env.Append(CPPPATH=["mylib/include"]) # this is a relative path
env.Append(CPPPATH=["#myotherlib/include"]) # this is an 'absolute' path
If you want to add custom compiler flags when building your module, you need to clone
`env` first, so it won't add those flags to whole Godot build (which can cause errors).
@@ -187,11 +184,13 @@ Example `SCsub` with custom flags:
.. code:: python
# SCsub
Import('env')
module_env = env.Clone()
module_env.add_source_files(env.modules_sources,"*.cpp")
module_env.Append(CXXFLAGS=['-O2', '-std=c++11'])
module_env.add_source_files(env.modules_sources, "*.cpp")
module_env.Append(CCFLAGS=['-O2']) # Flags for C and C++ code
module_env.Append(CXXFLAGS=['-std=c++11']) # Flags for C++ code only
And finally, the configuration file for the module, this is a simple
python script that must be named ``config.py``:
@@ -206,8 +205,8 @@ python script that must be named ``config.py``:
def configure(env):
pass
The module is asked if it's ok to build for the specific platform (in
this case, True means it will build for every platform).
The module is asked if it's OK to build for the specific platform (in
this case, ``True`` means it will build for every platform).
And that's it. Hope it was not too complex! Your module should look like
this:
@@ -266,6 +265,7 @@ library that will be dynamically loaded when starting our game's binary.
.. code:: python
# SCsub
Import('env')
sources = [
@@ -275,7 +275,7 @@ library that will be dynamically loaded when starting our game's binary.
# First, create a custom env for the shared library.
module_env = env.Clone()
module_env.Append(CXXFLAGS='-fPIC') # Needed to compile shared library
module_env.Append(CCFLAGS=['-fPIC']) # Needed to compile shared library
# We don't want godot's dependencies to be injected into our shared library.
module_env['LIBS'] = []
@@ -313,6 +313,7 @@ using the `ARGUMENT` command:
.. code:: python
# SCsub
Import('env')
sources = [
@@ -321,11 +322,12 @@ using the `ARGUMENT` command:
]
module_env = env.Clone()
module_env.Append(CXXFLAGS=['-O2', '-std=c++11'])
module_env.Append(CCFLAGS=['-O2'])
module_env.Append(CXXFLAGS=['-std=c++11'])
if ARGUMENTS.get('summator_shared', 'no') == 'yes':
# Shared lib compilation
module_env.Append(CXXFLAGS='-fPIC')
module_env.Append(CCFLAGS=['-fPIC'])
module_env['LIBS'] = []
shared_lib = module_env.SharedLibrary(target='#bin/summator', source=sources)
shared_lib_shim = shared_lib[0].name.rsplit('.', 1)[0]
@@ -335,7 +337,7 @@ using the `ARGUMENT` command:
# Static compilation
module_env.add_source_files(env.modules_sources, sources)
Now by default ``scons`` command will build our module as part of godot's binary
Now by default ``scons`` command will build our module as part of Godot's binary
and as a shared library when passing ``summator_shared=yes``.
Finally you can even speedup build further by explicitly specifying your

View File

@@ -37,38 +37,51 @@ if env['platform'] == '':
print("No valid target platform selected.")
quit();
# 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
# Check our platform specifics
if env['platform'] == "osx":
env['target_path'] += 'osx/'
cpp_library += '.osx'
if env['target'] in ('debug', 'd'):
env.Append(CCFLAGS = ['-g','-O2', '-arch', 'x86_64'])
env.Append(LINKFLAGS = ['-arch', 'x86_64'])
env.Append(CCFLAGS=['-g', '-O2', '-arch', 'x86_64'])
env.Append(LINKFLAGS=['-arch', 'x86_64'])
else:
env.Append(CCFLAGS = ['-g','-O3', '-arch', 'x86_64'])
env.Append(LINKFLAGS = ['-arch', 'x86_64'])
env.Append(CCFLAGS=['-g', '-O3', '-arch', 'x86_64'])
env.Append(LINKFLAGS=['-arch', 'x86_64'])
elif env['platform'] in ('x11', 'linux'):
env['target_path'] += 'x11/'
cpp_library += '.linux'
if env['target'] in ('debug', 'd'):
env.Append(CCFLAGS = ['-fPIC', '-g3','-Og', '-std=c++17'])
env.Append(CCFLAGS=['-fPIC', '-g3', '-Og'])
env.Append(CXXFLAGS=['-std=c++17'])
else:
env.Append(CCFLAGS = ['-fPIC', '-g','-O3', '-std=c++17'])
env.Append(CCFLAGS=['-fPIC', '-g', '-O3'])
env.Append(CXXFLAGS=['-std=c++17'])
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(ENV=os.environ)
env.Append(CCFLAGS = ['-DWIN32', '-D_WIN32', '-D_WINDOWS', '-W3', '-GR', '-D_CRT_SECURE_NO_WARNINGS'])
env.Append(CPPDEFINES=['WIN32', '_WIN32', '_WINDOWS', '_CRT_SECURE_NO_WARNINGS'])
env.Append(CCFLAGS=['-W3', '-GR'])
if env['target'] in ('debug', 'd'):
env.Append(CCFLAGS = ['-EHsc', '-D_DEBUG', '-MDd', '-ZI'])
env.Append(LINKFLAGS = ['-DEBUG'])
env.Append(CPPDEFINES=['_DEBUG'])
env.Append(CCFLAGS=['-EHsc', '-MDd', '-ZI'])
env.Append(LINKFLAGS=['-DEBUG'])
else:
env.Append(CCFLAGS = ['-O2', '-EHsc', '-DNDEBUG', '-MD'])
env.Append(CPPDEFINES=['NDEBUG'])
env.Append(CCFLAGS=['-O2', '-EHsc', '-MD'])
if env['target'] in ('debug', 'd'):
cpp_library += '.debug'