Building cURL Applications Using Tizen Studio

This topic describes how to build a sample application with Tizen Studio. The sample application uses Tizen Sockets Extension APIs and is based on the url2file cURL demo.


Related Info


Prerequisites

Before you can build the sample application:

  1. Install the Samsung Emscripten SDK as described in WebAssembly Overview.
  2. Install Tizen Studio as described in Quick-start Guide.
  3. Set up Tizen Studio and the Samsung Emscripten SDK toolchain as described in Creating WebAssembly Applications.

Building the Application

To build a sample application based on url2file cURL demo with Tizen Studio:

  1. Launch Tizen Studio.

  2. Create a project in Tizen Studio:

    1. Select "File > New > Tizen Project".
    2. Select "Template" and click "Next".
    3. Select "TV" and click "Next".
    4. Select "Web Application", check the "WebAssembly (C/C++)" checkbox and click "Next".
    5. Select "Empty Project" and click "Next".
    6. Give a name to your project (such as "HelloCURL") and click "Next".
    7. Provide the required Samsung Emscripten SDK toolchain paths and click "Finish".
  3. Add the http://tizen.org/privilege/internet privilege to your application. For more information, see Configuring Web Applications.

  4. Add a new WebAssembly module to your project:

    1. Right-click on your project in "Project Explorer".
    2. From the context menu, select "New > WebAssembly Module".
    3. Choose "Empty module" and click "Next".
    4. Give a name to your module (such as "HelloCURL_module").
    5. Add a "TextArea" by selecting the "On" checkbox. This “TextArea” displays the logs the WebAssembly module writes to stdout.
    6. Click "Finish".
  5. Either replace the contents of src/empty.cpp file in the created WebAssembly module with the contents of url2file_side_thread.cpp or just copy the "url2file_side_thread.cpp" file to the src/ directory and delete the src/empty.cpp file.

  6. Download CA certificates extracted from Mozilla and save them in the main directory of the WebAssembly module project.

  7. Add necessary compiler flag to your WebAssembly module:

    1. Right-click on your WebAssembly module project.

    2. Select "Properties" from the context menu.

    3. Select "C/C++ Build > Settings".

    4. On the "Tool Settings" tab, select "Emscripten C++ compiler > "Miscellaneous".

    5. Append the following flag to the "Other flags" field:

      -s USE_CURL=1
      

      The-s USE_CURL=1 flag is required here and in the linker flags below. As a compiler flag it is required to make cURL include directories populated and provided to compilation stage (using the -I switch). Also, putting this flag here allows the indexer to see cURL-included directories and properly resolve cURL includes.

  8. Add necessary linker flags to your WebAssembly module:

    1. Right-click on your WebAssembly module project.

    2. Select "Properties" from the context menu.

    3. Select "C/C++ Build > Settings".

    4. On the "Tool Settings" tab, select "Emscripten C++ linker > "Miscellaneous".

    5. Append the following flags to the "Linker flags" field:

      -s ENVIRONMENT_MAY_BE_TIZEN -s USE_CURL=1 --preload-file ../cacert.pem@/cacert.pem -pthread -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=2
      
      Flag Description
      -s ENVIRONMENT_MAY_BE_TIZEN

      Flag indicating that you want to use Samsung Tizen Emscripten extensions available on Samsung Tizen TVs. This flag is necessary to use POSIX sockets APIs in your application.
      -s USE_CURL=1

      Flag indicating that you want to build and use the cURL library provided with the Samsung Emscripten SDK toolchain. Note that during the first build, the full library is being built and cached, so compilation can take a while. For more details, see Emscripten Ports

      --preload-file ../cacert.pem@/cacert.pem

      Flag allowing your application to read the "cacert.pem" file using the standard C API fopen(./cacert.pem). Tizen Studio launches the Samsung Emscripten toolchain in a CurrentBin directory, although youn have the cacert.pem file in the main project's directory. To avoid copying this file, use a relative path. To refer to this file as /cacert.pem in a C/C++ program, use the@/cacert.pem mapping, as described in Modifying file locations in the virtual file system

      -pthread -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=2

      Flag allowing you to use threads in a WebAssembly module. Note that it is necessary to call POSIX sockets APIs in threads other than the main thread of your application.

      Table 1: Linker flags

  9. Build your WebAssembly module project.

  10. Apply the following workarounds:

    1. Unlink the WebAssembly module from main application. Right-click on the main application, go to "Others > Project References", and unselect the referenced WebAssembly module project.
    2. While in the project properties, add the "*.data" file to the WGT package. In "Tizen Studio > Package", expand the tree to the 'CurrentBin' directory of the WebAssembly module project and check the file with the data extension. Click "Apply and Close". Note this particular step must be performed after each build of the WebAssembly module project.
    3. Add the following snippet to wasm_modules/scripts/wasm_tools.js after the body print() method:
      locateFile: ( () => {
        return (path, prefix) => {
          if (prefix == '') {
            prefix = this.path.substring(0, this.path.lastIndexOf('/')) |
       '';
            prefix = prefix + '/';
          }
          return prefix + path;
        };
      })(),
      
  11. Build a signed package.

  12. Now you are done and can run your application on the device.