Repository navigation
Defines and Custom Targets
The IDE evaluates #if blocks and picks target-specific module files based on a set of defines. This page explains
where those defines come from, how you add, change or remove defines for the IDE without touching your build files,
how to set a custom target and how files named Module.<target>.hx are chosen.
How the editor shows active and inactive #if branches is covered in
Conditional Compilation.
The IDE builds a set of defines for each build file. A source file's #if blocks are evaluated with the set of the
build file it is analyzed under (see Resolve Context). A build file's set holds:
- The defines the build file sets for its selected target or section:
-Dlines and-debug(which definesdebug) in hxml; for a Lime, OpenFL or HXP project, the-Ddefines the build passes to the compiler (<haxedef>and<define>, not<set>); for an NME project, the defines of its evaluation. A dashed name such as-D my-flagdefinesmy_flag, as in the compiler. - The defines the Haxe compiler sets for that target, for example
jsfor JavaScript,cppandsysfor C++, andtarget.name. - One define per library, such as
mylib=1.2.0, and the-Dlines of the library'sextraParams.hxml, ashaxelib pathreports them for the compiler. They appear after the libraries have been resolved, which happens each time a trusted project opens; see Libraries and Haxelib. -
haxe_verandhaxe, plushaxe4and similar flags. They follow the module's language level or its SDK's version, depending on Use language level for conditional compilation (haxe_ver) in Settings | Build, Execution, Deployment | Compiler | Haxe Compiler. -
hl_ver, read from the SDK's standard library, unless the build file sets it. - The custom target of the build file, if one is set and the build selects no target of its own (see Custom targets).
- The define overrides of the build file (see below).
The Defines group under a build file in the Haxe tool window lists only the first kind. Where the IDE has no file to go by, it uses the active build file's set; if no build file is active, none of the above applies there.
A define override adds, changes or removes one define, for the IDE only. Each build file has its own overrides, under
its IDE Overrides row in the Haxe tool window. Use them to read and edit code as it would look in another
configuration, without editing the build file. The overrides are saved with the project, in
.idea/haxeBuildConfig.xml, and follow the build file when you move or rename it.
Important
Define overrides are never added to build commands. Building, running and testing use only your build files.
flowchart LR
build["Build file"]
overrides["IDE Overrides<br/>of the build file"]
runs["Builds, Build and run,<br/>test runs"]
editor["Editor analysis:<br/>conditional branches, resolve"]
compiler["Compiler-backed features:<br/>diagnostics, completion"]
build --> runs
build --> editor
build --> compiler
overrides --> editor
overrides --> compiler
Overrides apply in two places:
-
The editor's own analysis, such as
#ifevaluation, highlighting and resolve, uses the overrides of the build file the edited file is analyzed under. -
Compiler-backed features, such as compiler diagnostics and completion, send a build file's overrides along with
its build configuration, so the compiler sees the same
#ifbranches as the editor. See Compilation Server.
Each override has one of three actions:
| Action | Effect |
|---|---|
| Add | Defines a name that the build file does not set, with the value you give. |
| Override | Gives a define that the build file sets a different value. |
| Remove | Removes the define, typically one that the build file sets. |
Add and Override are the same kind of entry; the IDE shows whichever applies to the build file. An entry
without a value defines a flag with the value 1, the same as -D name on the command line.
The overrides are listed under the build file's IDE Overrides | Defines:
- A define shows as
name = value. If the build file sets the same define, the row adds (overrides build file). - A Remove entry shows the name struck through, followed by Remove.
To change them:
- To add a define, right-click IDE Overrides or its Defines row and choose Add Define…. In the
Haxe Define dialog, enter
nameorname=value. The name must not be empty or contain spaces. - To remove a define that the build file sets, choose Add Undefine… in the same menu. In the Haxe Undefine dialog, enter the name under Define name to remove:.
- To change one, right-click it and choose Edit Define….
- To delete one, right-click it and choose Remove Define, or select it and press Delete. Both ask first.
Neither dialog accepts haxe_ver, haxe, haxe3, haxe4 or haxe5: these identify the compiler and follow the
language level or the compiler version. The dialog says so, for example
haxe_ver identifies the compiler and cannot be overridden.
You can also toggle a define from the editor: on a flag inside an #if condition, the
Define flag 'name' for build file and Undefine flag 'name' for build file context actions add or remove
an override of the build file the edited file is analyzed under. They are offered only when the file has one, and not
for haxe_ver, haxe, haxe3, haxe4 and haxe5. See Conditional Compilation.
Right-click IDE Overrides, or any row under it, and choose Manage IDE Overrides…. The IDE Overrides: build file dialog edits the build file's define overrides and custom target:
| Field | What it sets |
|---|---|
| Defines table | One row per override, with the columns Name, Value and Action (Add, Override or Remove). Use the table's toolbar to add and delete rows. A Remove row has no value. Rows without a name are dropped. |
| Custom target: | The build file's custom target; leave empty for none. Only for hxml files. |
Click OK to save.
Haxe 5 can compile for a target that is not built into the compiler, with the --custom-target flag. Haxe 4.3 does
not have this flag. For such a target, the IDE does what the compiler does:
- defines
custom_target; - sets
target.nameto the target's name, so you test for it with#if (target.name == "mytarget"). The name itself is not defined as a flag, so#if mytargetstays false; - uses module files named after the target, such as
Socket.mytarget.hx(see below).
The IDE learns about a custom target in two ways:
- From the build file: an hxml line
--custom-target mytarget(or--custom-target mytarget=path). The file's Target row then shows Custom (mytarget). - From the Custom target row under an hxml file's IDE Overrides: click its value and enter the name in the Custom Target dialog, or use the Custom target: field of Manage IDE Overrides…. Leave it empty to clear it.
The Custom target override is meant for an hxml file that selects no target, which the compiler would otherwise
compile for its default platform. It applies only while the file's selected section has no target flag such as --js
or --hl, no --custom-target and no --run. When the build selects a target of its own, that target wins, and the
row shows the custom target struck through, followed by (build sets target), for example
(build sets JavaScript).
The editor's own analysis uses the custom target for the files analyzed under that build file. Compiler-backed features
get it as --custom-target mytarget when the module's Haxe SDK is Haxe 5 or later. It is never passed to build
commands; to build for a custom target, put --custom-target in the build file.
The name must follow the compiler's rules, which both the Custom Target dialog and the Custom target: field of Manage IDE Overrides… check, in this order, each with its own message:
- it is not the name of a built-in target:
js,lua,neko,flash,php,cpp,jvm,python,hloreval(js is a built-in target name); - it has at most 16 characters;
- it starts with a lowercase letter or
_and contains only ASCII letters, digits and_.
Since Haxe 4.0, a module can have target-specific versions: when the compiler finds <Module>.hx, it uses
<Module>.<target>.hx from the same folder instead, if that file exists. See the Haxe manual on
target-specific files.
The IDE follows the same rules:
- A file such as
Socket.js.hxalways declares the moduleSocket, neverSocket.js, whichever target is selected. Imports and type references are written the same way for every target. - The variant is active when its suffix is the platform name of the current target, that is, when the IDE's
defines contain it (
js,hl,cpp,neko,python,lua,php, ...), or whentarget.nameequals the suffix, as it does for a custom target. - An active variant is used instead of the plain file. Otherwise the plain file is used. If there is no plain file, the IDE still uses the variant, so references resolve; the compiler, however, requires the plain file.
- Resolve, navigation and completion all follow the active variant, so you always work with the file the compiler would use.
Which variant is used follows the active build file's defines, for every file. Switching the active build file, its target or its section switches the variants too.
src/
└── net/
├── Socket.hx used for every other target
├── Socket.js.hx used for JavaScript
└── Socket.hl.hx used for HashLink
Each file declares the same class:
// src/net/Socket.js.hx
package net;
class Socket {
public function new() {}
public function send(data:String):Void {
// JavaScript implementation
}
}Code that uses it does not change:
// src/Main.hx
import net.Socket;
class Main {
static function main() {
new Socket().send("hello");
}
}With an active build.hxml that contains -js bin/app.js, Go to Declaration on Socket in Main.hx opens
Socket.js.hx. Change the build to -hl bin/app.hl and it opens Socket.hl.hx; with -neko bin/app.n it opens
Socket.hx. For a custom target named mytarget, the variant file would be Socket.mytarget.hx.
Related pages: Build Files, Targets and Sections, The Haxe Tool Window.
Getting started
Working with projects
- The Haxe Tool Window
- Build Files
- Targets and Sections
- Resolve Context
- Libraries and Haxelib
- Defines and Custom Targets
- Building and Running
- Run Configurations
- Tools
- Compilation Server
Debugging
Testing
Profiling
Editing code
- Code Completion
- Diagnostics and Inspections
- Refactoring and Navigation
- Formatting and Code Style
- Conditional Compilation
- Editor Extras
Reference