Chapter 48. Understanding Compilation

Table of Contents

1. Overview
1.1. The Developer's Console
2. The Command Line Compiler
2.1. Running the Command Line Compiler
2.2. Command Line Options
2.3. Compile-time constants
2.4. New DHTML Runtime and new URL parameters
3. Compiler Errors and Warnings
4. Compiler Directives
4.1. Conditional Compilation of Runtime-Dependent code
4.2. Font and Face

You can compile your applications for any of several runtimes using any of several techniques. In this chapter we'll look at the various ways of invoking the compiler and its various options:

1. Overview

You can cause compilation either by implicitly calling the compiler through the OpenLaszlo Server, or by explicitly calling it from the command line. The most usual way to invoke the server is using the Developer's Console.

1.1. The Developer's Console

The Developer's Console is a small OpenLaszlo application for selecting things like the target runtime, the deployment mode (proxied or SOLO) and whether the debugger is included. When you first compile an OpenLaszlo application (as explained below), by default it is returned with the Developer's Console appearing at the bottom of the application.

Depending on whether your compile to SWF (the default) or DHTML, a slightly different version of the Developer's Console is returned. The illustrations below call out features of both versions.

2. The Command Line Compiler

In OpenLaszlo, 'the compiler' consists (currently) of two phases: 1) 'the view compiler', which compiles LZX to Javascript, and 2) 'the script compiler', which compiles Javascript to the target runtime (SWF7, SWF8 and DHTML). `lzc` invokes both those phases (although you can ask for the intermediate output).

The Debugger needs an OL server to be able to evaluate forms (it calls the server to compile the form and then loads and runs the result). It will try to talk to OL server (using the url the application was loaded from), so it should 'just work', so long as you are running a server, even if you don't use the server for compiling your application.

2.1. Running the Command Line Compiler

To run lzc you must do so from a current directory that matches the value of LPS_HOME, and LPS_HOME must be set to the correct value for the build.

Say the application to be compiled is

$LPS_HOME/laszlo-explorer/basics/mediatst.lzx

To compile it correctly you would set the current directory first:

cd $LPS_HOME;

and then compile using

lzc --script --runtime=dhtml laszlo-explorer/basics/mediatst.lzx;

Let's say the file $LPS_HOME/laszlo-explorer/basics/mediatst.lzx wants to reference the file $LPS_HOME/laszlo-explorer/basics/assets/background.jpg. It might contain the following reference.

<canvas>
  <view resource="assets/background.jpg"/>
</canvas>

Running LZC from other than LPS_HOME will result in a file not found error.

2.2. Command Line Options

Here are the options available for compilation:

Options:

-D<name>=<value>

Set the name/var property to value (See Compiler.getProperties).

-D<name>

Short for -Dname=true.

-v

Write progress information to standard output.

--mcache on|off

Turns on/off media cache. Default is off.

--onerror [throw|warn]

Action to take on compilation errors. Defaults to warn.

--help

lists all options.

--flush-script-cache

Doesn't flush script cache before compiling.

Output options:

--runtime=[swf7|swf8|dhtml]

Compile to swf7, swf8, or dhtml.

--dir outputdir

Output directory.

-g | --debug

Add debugging information into the output object.

-p | --profile

Add profiling information into the output object.

Logging options:

-l<loglevel>

Logging level (See org.apache.log4j.Level)

-l<loggerName>=<loglevel>

Logging level (See org.apache.log4j.Level)

-lp file

Log4j properties files

--log logfile

Specify logfile (output still goes to console as well)

--schema

Writes the schema to standard output.

--script

Writes JavaScript to standard output.

2.3. Compile-time constants

You can define compile-time constants to `lzc` by using `-D_name_=_value_`, where _value_ should be either `true` or `false`. By convention, we use `$` as the first character of _name_, but nothing enforces that.

The compiler will compile only the chosen branch of an if statement when the condition expression is a compile-time constant (i.e., just the constant name, no other computation). For example:

  if ($slow) {
    ... slow way ...
  } else {
    ... fast way ...
  }

You can define compile-time constants to `lzc` by using `-D_name_=_value_`, where _value_ should be either `true` or `false`. By convention, we use `$` as the first character of _name_, but nothing enforces that. The compiler will compile only the chosen branch of an if statement when the condition expression is a compile-time constant (i.e., just the constant name, no other computation).

For example:

  if ($slow) {     ... slow way ...   } else {     ... fast way ...   } Can be made to run fast by `lzc -Dslow=false`

2.4.  New DHTML Runtime and new URL parameters

As part of the architectural overhaul, we have created a new version of the client runtime (LFC plus kernel). To invoke an application compiled for DHMTML, use the ?lzr=dhtml query.

The ?lzr parameter selects the runtime. Your choices are swf7, swf8, dhtml. The default is specified in lps/config/lps.properties as compiler.runtime.default. The default runtime selector is swf7.

The ?lzt parameter is the 'request type', in general what kind of 'wrapper' page will be generated around your application. Your choices are: app_console or html. app_console is the developer console, html is your app in an html page. The latter can be used as a model for embedding your app in a custom page, or you can simply embed using an iframe:

<iframe src=".../app.lzx?lzt=html"></iframe>

The default for ?lzt is specified in lps/config/lps.properties as defaultRequestType. The default value is app_console.

Mnemonics:

  • lzr - Laszlo Runtime

  • lzt - Laszlo request Type

So, for example, to view an LZX application in DHTML within a browser, download it from a live server with an url parameter requesting the DHTML runtime lzt=html: http://myserver/mylaszloapp.lzx?lzr=dhtml. To request the SWF runtime, use http://myserver/mylaszloapp.lzx?lzr=swf.

To specify that the application run in the Developer's Console, use the lzt flag: lzt=app_console loads the application with the Developer's Console. lzr=swf7 and lzr=swf8 loads the application compiled for the SWF7 or SWF8 runtime, with the Developer's Console. Several lzr values have been added for future use, but are not currently supported: swf9, j2me, svg

3. Compiler Errors and Warnings

When the compiler detects an error that makes it impossible to compile the application, the error is reported in the browser window, or, if you invoked compilation using lzc, in the terminal window.

When the compiler detects code that may be problematic but does not prevent compilation, it issues a warning. If you have debugging enabled, the warning is displayed there. See Chapter 50, Debugging for a detailed discussion of how to detect errors.

4. Compiler Directives

4.1. Conditional Compilation of Runtime-Dependent code

You can enclose blocks of code within <switch> elements that are associated with a specific runtime. The <switch> is a compiler directive that instructs the compiler to compile or omit blocks according to the target runtime to which the application is being compiled. Within a <switch> block, use the <when> to specify the runtime.

Example 48.1. switch tag for condtional compilation

<canvas height="20">
  <switch>
    <when runtime="dhtml">
       <text>This program has been compiled for DHTML</text>
    </when>
    <otherwise>
      <text>This program has been compiled for SWF</text> 
    </otherwise>
  </switch>
</canvas>

4.2. Font and Face

The <font> and <face> tags are also compiler directives. Unlike most other LZX tags, they do not correspond to a class; rather, they tell the compiler which fonts and typefaces are to be bound to objects.