Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Limitations of the Web platform
The main limitations to keep in mind that are introduced when targeting a Web application are:
- No real multithreading model is present (although a model similar to multiprocessing is available thanks to WebWorkers)
- No filesystem access (besides files that the user has explicitly selected)
The Cheerp compiler is developed following the philosophy of exposing both the features and limitations of the Web platform to the application. As such, any function exposed by the Web browser is available to be accessed from C/C++ code compiled with Cheerp, and conversely, all of its architectural limitations are exposed.
Limitations introduced by Cheerp
While most C++ applications, especially portable, multi-platform ones, have a sufficent level of type safety to be compilable with Cheerp, some C++ and many C applications and libraries will require some work to be portable using Cheerp. By default, Cheerp is quite verbose in reporting type safety issues, and its console output can be effectively used to guide the type-safety improvement efforts.
Unsupported code and workarounds
Custom allocators and type-unsafe pool allocators
A recommended workaround is to disable custom allocators and directly call
(struct destinationType*) malloc(sizeof(struct destinationType)) or
new destinationType(). Some C libraries (Zlib is an example) call
free indirectly through function pointers contained in a context structure. In such case you will need to replace the indirect call with direct calls to
malloc with a well defined type.
Code that requires a byte addressable space
C library functions such as
bsearch are not supported with Cheerp as they required a byte-addressable architecture. A workaround that can be used is to compile the C file as a C++ file and use C++ function equivalents, such as
std::binary_search from the C++ library.
Memory functions such as
memmove are supported, but the argument type must be known at compile time.
A recommended approach to porting an application
At Leaning Technologies we have built quite some experience in porting large-scale C/C++ code bases using Cheerp. In our opinion, the fastest, most effective way to get to a fully working ported application is to follow a top-down approach. Cheerp is designed to help in the porting process by providing verbose error and warning messages that highlight the code that is definitely (in case of errors) or possibly (in case of warnings) not supported on the Web platform.
The top-down approach that we recommend is as follows:
- Configure the build system to use the Cheerp compiler. Cheerp is derived from clang and it is mostly command line compatible with gcc/clang. Make sure to enable the
-target cheerpcommand line option.
- If your codebase is already multiplatform, use the Linux or Unix target as the starting point for the Cheerp version. Cheerp defines the
__CHEERP__macro to make it possible to integrate Cheerp code in the platform specific sections of your codebases.
- Get most of your cosebase to compile, by fixing - if immediately obvious - or commenting out sections using the
#ifndef __CHEERP__/#endifidiom. You will be able to work those sections back in the code base later in the process, allowing you to prioritize the different parts.
- This should get you quickly to a rough proof of concept, which you can integrate into a Web application and test.
- Fix type safety and platform specific issues as required to get your tests and benchmarks to run.
- Gradually reintroduce the commented sections of your code into the code base and test them, until you get to your complete application.
Commenting out code
#ifndef __CHEERP__/#endif idiom to proceed further with the porting.
If you need to comment out subsections of a method we recommend the following idiom to avoid potential issues.
#ifdef __CHEERP__ assert(false && "Missing support for ..."); #else // Your original code, it will still compile if you switch to a native target #endif
While commenting out code is effective to evaluate the scale of the required porting work and quickly getting to a proof-of-concept state, it should be kept in mind that it can also lead to issues that are difficult to debug, and a careful use of a version control system will help mitigating the risks of losing track of the root cause of some problems.