- Joined
- Jun 21, 2017
- Messages
- 87
This tutorial is for windows only.
1. Compile and run an example
The first thing to make a target is to get a very simple example to compile and run. So in this tutorial the example is:
And to compile it, the command is:
And use your favorite command line:
So if you want to create a new target, the first thing to do is to get one simple example to compile. Also how to compile that example.
2. Modify and compile transcc.
2a. Modify src\transcc\builders\builders.cxs
This is where you define your target. So add the following lines
2b. Create src\transcc\builders\hello.cxs
New is where this class is initialized. Config method is for those keywords like GLFW_WINDOW_WIDTH and GLFW_WINDOW_HEIGHT. We wont be using it. IsValid method is to check whether the directory path is defined in bin\config.winnt.txt. So in our case we want to use Mingw compiler. Begin method is what language do we translate Cerberus to cpp, java, javascript etc.
And MakeTarget is where the command line happens. In this example, we delete the executable. Then we compile the source file. Lastly we execute the newly executable.
2c. Compile transcc
Before you do this step, copy bin\transcc_winnt.exe to bin\transcc_winnt_original.exe. So you keep a backup of your original transcc before you modify it.
Now we need to compile this. So we need a transcc to create a transcc! The command to compile is:
Here is my command lines:
You need to copy transcc.build\cpptool\main_winnt.exe to bin\transcc_winnt.exe. So usually I create a batch file called c.bat like this:
2d. Start to create the hello target. Create targets\hello\target.cxs
Create a directory call hello in the targets directory. Then create the file target.cxs:
Now if you run transcc_winnt.exe, you will see Hello_Tool as a valid target:
3. Compiling the simple example from step 1 using the new transcc
3a. Copy d:\devtools\hello\main.cpp to targets\hello\template\main.cpp.
You will need to create the directory template in the hello target directory.
3b. Creating and compiling hello.cxs
Now open Ted and create hello.cxs:
And also set the build target to Hello Tool. And run it, you should see it prints out "Hello World From C++" like in this screenshot:
So at this point in the tutorial, our transcc is working and it can compile the example from step 1.
4. Compiling Print from Cerberus
4a. Let's change hello.cxs to be:
4b. targets\hello\template\main.cpp:
4c. targets\hello\template\main.h
main.h is from cpptool:
4d. src\transcc\builders\hello.cxs
Modify MakeTarget method to include making main.cpp. So this 4 lines of code translate Cerberus code to C++.
Remember to recompile the transcc using the batch file we created earlier.
4e. Compiling hello.cxs
If you try to compile hello.cxs, you will still see "Hello World From C++". This is because any changes you make in targets\hello\template directory is still the same in your current build directory. SO YOU HAVE TO REMEMBER TO DELETE THE YOUR BUILD DIRECTORY! If not your changes will not take effect.
Here is a screenshot of what you should see:
Summary
1. Get the simplest example compiling and running.
2. Create a new target in transcc.
3. Use the example from step 1 and compile that with transcc.
4. Get Cerberus code compiling and running.
I tried to make this tutorial as simple as possible, so you have a good understanding of the basic of creating a target. Please let me know if there are any questions, comments, mistakes, and problems.
1. Compile and run an example
The first thing to make a target is to get a very simple example to compile and run. So in this tutorial the example is:
C++:
#include <stdio.h>
int main()
{
printf("Hello World From C++\n");
return 0;
}
And to compile it, the command is:
Code:
g++ -o main main.cpp
And use your favorite command line:
Code:
D:\devtools\hello>g++ -o main main.cpp
D:\devtools\hello>main
Hello World From C++
D:\devtools\hello>
So if you want to create a new target, the first thing to do is to get one simple example to compile. Also how to compile that example.
2. Modify and compile transcc.
2a. Modify src\transcc\builders\builders.cxs
This is where you define your target. So add the following lines
Code:
Import xna
Import hello
...
builders.Set "xna",New XnaBuilder( tcc )
builders.Set "hello",New HelloBuilder( tcc )
2b. Create src\transcc\builders\hello.cxs
Code:
Import builder
Class HelloBuilder Extends Builder
Method New( tcc:TransCC )
Super.New( tcc )
End
Method Config:String()
Local config:=New StringStack
For Local kv:=Eachin GetConfigVars()
config.Push "#define CFG_"+kv.Key+" "+kv.Value
Next
Return config.Join( "~n" )
End
Method IsValid:Bool()
Select HostOS
Case "winnt"
If tcc.MINGW_PATH Return True
Default
Return True
End
Return False
End
Method Begin:Void()
ENV_LANG="cpp"
_trans=New CppTranslator
End
Method MakeTarget:Void()
Local out:="main"
DeleteFile out
Execute "g++ -o " + out + " main.cpp"
If tcc.opt_run
Execute "~q" + RealPath( out ) + "~q"
Endif
End
End
New is where this class is initialized. Config method is for those keywords like GLFW_WINDOW_WIDTH and GLFW_WINDOW_HEIGHT. We wont be using it. IsValid method is to check whether the directory path is defined in bin\config.winnt.txt. So in our case we want to use Mingw compiler. Begin method is what language do we translate Cerberus to cpp, java, javascript etc.
And MakeTarget is where the command line happens. In this example, we delete the executable. Then we compile the source file. Lastly we execute the newly executable.
2c. Compile transcc
Before you do this step, copy bin\transcc_winnt.exe to bin\transcc_winnt_original.exe. So you keep a backup of your original transcc before you modify it.
Now we need to compile this. So we need a transcc to create a transcc! The command to compile is:
Code:
transcc_winnt.exe -target=C++_Tool transcc.cxs
Here is my command lines:
Code:
D:\devtools\Cerberus\src\transcc>d:\devtools\Cerberus\bin\transcc_winnt.exe -taget=C++_Tool transcc.cxs
TRANS cerberus compiler V2017-07-04
Parsing...
Semanting...
Translating...
Building...
D:\devtools\Cerberus\src\transcc>
You need to copy transcc.build\cpptool\main_winnt.exe to bin\transcc_winnt.exe. So usually I create a batch file called c.bat like this:
Code:
d:\devtools\Cerberus\bin\transcc_winnt.exe -target=C++_Tool transcc.cxs
copy d:\devtools\Cerberus\src\transcc\transcc.buildv2017-07-04\cpptool\main_winnt.exe d:\devtools\Cerberus\bin\transcc_winnt.exe
pause
2d. Start to create the hello target. Create targets\hello\target.cxs
Create a directory call hello in the targets directory. Then create the file target.cxs:
Code:
#TARGET_NAME="Hello Tool"
#TARGET_SYSTEM="hello"
#TARGET_BUILDER="hello"
Now if you run transcc_winnt.exe, you will see Hello_Tool as a valid target:
Code:
D:\devtools\Cerberus>bin\transcc_winnt.exe
TRANS cerberus compiler V2017-07-04
TRANS Usage: transcc [-update] [-build] [-run] [-clean] [-config=...] [-target=...] [-cfgfile=...] [-modpath=...] <main_cerberus_source_file>
Valid targets: C++_Tool Desktop_Game_(Glfw3) Desktop_Game_(Glfw3+Angle) Hello_Tool Html5_Game Windows_RT_Game_(Phone) Windows_RT_Game_(Tablet)
Valid configs: debug release
3. Compiling the simple example from step 1 using the new transcc
3a. Copy d:\devtools\hello\main.cpp to targets\hello\template\main.cpp.
You will need to create the directory template in the hello target directory.
C++:
#include <stdio.h>
int main()
{
printf("Hello World From C++\n");
return 0;
}
3b. Creating and compiling hello.cxs
Now open Ted and create hello.cxs:
Code:
Function Main()
End
And also set the build target to Hello Tool. And run it, you should see it prints out "Hello World From C++" like in this screenshot:
So at this point in the tutorial, our transcc is working and it can compile the example from step 1.
4. Compiling Print from Cerberus
4a. Let's change hello.cxs to be:
Code:
Function Main()
Print "Hello World From Cerberus"
End
4b. targets\hello\template\main.cpp:
C++:
#include "main.h"
//${CONFIG_BEGIN}
//${CONFIG_END}
//${TRANSCODE_BEGIN}
//${TRANSCODE_END}
int main( int argc,const char **argv )
{
bb_std_main( argc,argv );
}
4c. targets\hello\template\main.h
main.h is from cpptool:
C++:
//Lang/OS...
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <typeinfo>
#include <signal.h>
#if _WIN32
#include <windows.h>
#include <direct.h>
#include <sys/stat.h>
#undef LoadString
#elif __APPLE__
#include <mach-o/dyld.h>
#include <sys/stat.h>
#include <dirent.h>
#include <copyfile.h>
#elif __linux
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pthread.h>
#endif
#define _QUOTE(X) #X
#define _STRINGIZE( X ) _QUOTE(X)
4d. src\transcc\builders\hello.cxs
Modify MakeTarget method to include making main.cpp. So this 4 lines of code translate Cerberus code to C++.
Code:
Method MakeTarget:Void()
' This create main.cpp and put the Cerberus code in there.
Local main:=LoadString( "main.cpp" )
main=ReplaceBlock( main,"TRANSCODE",transCode )
main=ReplaceBlock( main,"CONFIG",Config() )
SaveString main,"main.cpp"
Local out:="main"
DeleteFile out
Execute "g++ -o " + out + " main.cpp"
If tcc.opt_run
Execute "~q" + RealPath( out ) + "~q"
Endif
End
Remember to recompile the transcc using the batch file we created earlier.
4e. Compiling hello.cxs
If you try to compile hello.cxs, you will still see "Hello World From C++". This is because any changes you make in targets\hello\template directory is still the same in your current build directory. SO YOU HAVE TO REMEMBER TO DELETE THE YOUR BUILD DIRECTORY! If not your changes will not take effect.
Here is a screenshot of what you should see:
Summary
1. Get the simplest example compiling and running.
2. Create a new target in transcc.
3. Use the example from step 1 and compile that with transcc.
4. Get Cerberus code compiling and running.
I tried to make this tutorial as simple as possible, so you have a good understanding of the basic of creating a target. Please let me know if there are any questions, comments, mistakes, and problems.
Last edited by a moderator: