Skip to content

Commit

Permalink
Fix inconsistencies with define flags (#10365)
Browse files Browse the repository at this point in the history
* Fix inconsistency between - and _ in define.json

* Remove unnecessary dash conversion

define.json now already provides them with dashes so this manual
conversion is no longer necessary.

* Move `source-header` default value setting

* Split function with bool parameter

* Fix missing error when doing `-D haxe-ver`

* Use underscore defines internally

Move all define conversions to interfaces, and internally a flag is
represented using the underscore version only.

This prevents duplicate defines appearing in a bunch of places, and now
there are no longer tiny differences in the effects of `-D flag-a` and
`-D flag_a`. Same is true for macro functions for accessing defines.

* Use existing functions for accessing defines

* Remove duplicate definition of the `flash` define

This is already done for every target in `Common.init_platform`.

* Update documentation for `net-ver` compiler flag

* Remove `dump_context` made redundant in bef481f

* Clean up define outputs

All internal outputs of defines are now in alphabetical order

* Add tests for define flag conversions

* Add tests for `-D no-deprecation-warnings`

* Add tests for `-D source-header`

* Test for duplicate defines in `hlc.json` & `Options.txt`

* [hl] Fix `-D source-header` not working with hlc (#10376)

* Add tests for `source-header` on hlc

* Clean up

- Error for `-D haxe-ver` solved more generally
  - Add temporary function to be removed for #8690
- Minor refactoring

* Fix `--help-defines`
  • Loading branch information
tobil4sk authored Oct 12, 2021
1 parent 8735fcf commit 0ef19b4
Show file tree
Hide file tree
Showing 62 changed files with 797 additions and 303 deletions.
178 changes: 89 additions & 89 deletions src-json/define.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions src/codegen/dotnet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ let convert_ilfield ctx p field =
| CInitOnly | CLiteral -> true, acc
| _ -> readonly,acc
) (false,[cff_access]) field.fflags.ff_contract in
if PMap.mem "net_loader_debug" ctx.ncom.defines.Define.values then
if Common.raw_defined ctx.ncom "net_loader_debug" then
Printf.printf "\t%sfield %s : %s\n" (if List.mem_assoc AStatic acc then "static " else "") cff_name (IlMetaDebug.ilsig_s field.fsig.ssig);
let kind = match readonly with
| true ->
Expand Down Expand Up @@ -363,7 +363,7 @@ let convert_ilevent ctx p ev =
else
acc
in
if PMap.mem "net_loader_debug" ctx.ncom.defines.Define.values then
if Common.raw_defined ctx.ncom "net_loader_debug" then
Printf.printf "\tevent %s : %s\n" name (IlMetaDebug.ilsig_s ev.esig.ssig);
let acc = add_m acc ev.eadd in
let acc = add_m acc ev.eremove in
Expand Down Expand Up @@ -400,7 +400,7 @@ let convert_ilmethod ctx p is_interface m is_explicit_impl =
(APrivate,null_pos), ((Meta.Protected, [], p) :: meta)
| FAPublic -> (APublic,null_pos), meta
| _ ->
if PMap.mem "net_loader_debug" ctx.ncom.defines.Define.values then
if Common.raw_defined ctx.ncom "net_loader_debug" then
Printf.printf "\tmethod %s (skipped) : %s\n" cff_name (IlMetaDebug.ilsig_s m.msig.ssig);
raise Exit
in
Expand All @@ -412,7 +412,7 @@ let convert_ilmethod ctx p is_interface m is_explicit_impl =
| _ -> acc, is_final
) ([acc],None) m.mflags.mf_contract in
let acc = (AOverload,p) :: acc in
if PMap.mem "net_loader_debug" ctx.ncom.defines.Define.values then
if Common.raw_defined ctx.ncom "net_loader_debug" then
Printf.printf "\t%smethod %s : %s\n" (if !is_static then "static " else "") cff_name (IlMetaDebug.ilsig_s m.msig.ssig);

let acc = match is_final with
Expand Down Expand Up @@ -551,7 +551,7 @@ let convert_ilprop ctx p prop is_explicit_impl =
| _ -> "never");
| Some _ -> "set"
in
if PMap.mem "net_loader_debug" ctx.ncom.defines.Define.values then
if Common.raw_defined ctx.ncom "net_loader_debug" then
Printf.printf "\tproperty %s (%s,%s) : %s\n" prop.pname get set (IlMetaDebug.ilsig_s prop.psig.ssig);
let ilsig = match prop.psig.snorm with
| LMethod (_,ret,[]) -> ret
Expand Down Expand Up @@ -717,7 +717,7 @@ let convert_ilclass ctx p ?(delegate=false) ilcls = match ilcls.csuper with
| _ ->
let flags = ref [HExtern] in
(* todo: instead of CsNative, use more specific definitions *)
if PMap.mem "net_loader_debug" ctx.ncom.defines.Define.values then begin
if Common.raw_defined ctx.ncom "net_loader_debug" then begin
let sup = match ilcls.csuper with | None -> [] | Some c -> [IlMetaDebug.ilsig_s c.ssig] in
let sup = sup @ List.map (fun i -> IlMetaDebug.ilsig_s i.ssig) ilcls.cimplements in
print_endline ("converting " ^ ilpath_s ilcls.cpath ^ " : " ^ (String.concat ", " sup))
Expand Down Expand Up @@ -1141,14 +1141,14 @@ class net_library com name file_path std = object(self)
let cache = IlMetaReader.create_cache () in
let meta = IlMetaReader.read_meta_tables ctx clr_header cache in
close_in (r.PeReader.ch);
if PMap.mem "net_loader_debug" com.defines.Define.values then
if Common.raw_defined com "net_loader_debug" then
print_endline ("for lib " ^ file_path);
let il_typedefs = Hashtbl.copy meta.il_typedefs in
Hashtbl.clear meta.il_typedefs;

Hashtbl.iter (fun _ td ->
let path = IlMetaTools.get_path (TypeDef td) in
if PMap.mem "net_loader_debug" com.defines.Define.values then
if Common.raw_defined com "net_loader_debug" then
Printf.printf "found %s\n" (s_type_path (self#netpath_to_hx path));
Hashtbl.replace com.net_path_map (self#netpath_to_hx path) path;
Hashtbl.replace meta.il_typedefs path td
Expand Down Expand Up @@ -1190,7 +1190,7 @@ class net_library com name file_path std = object(self)
let pack = match fst path with | ["haxe";"root"] -> [] | p -> p in
let cp = ref [] in
let rec build path = try
if PMap.mem "net_loader_debug" com.defines.Define.values then
if Common.raw_defined com "net_loader_debug" then
Printf.printf "looking up %s\n" (s_type_path path);
match self#lookup path with
| Some({csuper = Some{snorm = LClass( (["System"],[],("Delegate"|"MulticastDelegate")),_)}} as cls)
Expand Down Expand Up @@ -1241,12 +1241,12 @@ let add_net_lib com file std extern =

let before_generate com =
(* netcore version *)
let netcore_ver = try Some(PMap.find "netcore_ver" com.defines.Define.values) with Not_found -> None in
let netcore_ver = try Some(Common.defined_value com Define.NetcoreVer) with Not_found -> None in

(* net version *)
let net_ver =
try
let ver = PMap.find "net_ver" com.defines.Define.values in
let ver = Common.defined_value com Define.NetVer in
try int_of_string ver with Failure _ -> raise (Arg.Bad "Invalid value for -D net-ver. Expected format: xx (e.g. 20, 35, 40, 45, 50)")
with Not_found when netcore_ver != None ->
(* 4.7 was released around .NET core 2.1 *)
Expand Down Expand Up @@ -1274,7 +1274,7 @@ let before_generate com =

(* net target *)
let net_target = try
String.lowercase (PMap.find "net_target" com.defines.Define.values)
String.lowercase (Common.defined_value com Define.NetTarget)
with | Not_found ->
"net"
in
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/gencommon/realTypeParams.ml
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ let default_implementation gen (dyn_tparam_cast:texpr->t->texpr) ifaces =
false)
| _ -> false
in
let unifies = unifies && not (PMap.mem "cs_safe_casts" gen.gcon.defines.Define.values) in
let unifies = unifies && not (Common.raw_defined gen.gcon "cs_safe_casts") in
(match follow t with
| TInst(cl, p1 :: pl) when is_hxgeneric (TClassDecl cl) && not unifies && not (Meta.has Meta.Enum cl.cl_meta) ->
let iface = Hashtbl.find ifaces cl.cl_path in
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/java.ml
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ let convert_java_enum ctx p pe =
String.concat "_" parts,
(Meta.Native, [EConst (String (cff_name,SDoubleQuotes) ), cff_pos], cff_pos) :: !cff_meta
in
if PMap.mem "java_loader_debug" ctx.jcom.defines.Define.values then
if Common.raw_defined ctx.jcom "java_loader_debug" then
Printf.printf "\t%s%sfield %s : %s\n" (if List.mem_assoc AStatic !cff_access then "static " else "") (if List.mem_assoc AOverride !cff_access then "override " else "") cff_name (s_sig field.jf_signature);

{
Expand Down Expand Up @@ -453,7 +453,7 @@ let convert_java_enum ctx p pe =
[convert_java_enum ctx p jc]
| false ->
let flags = ref [HExtern] in
if PMap.mem "java_loader_debug" ctx.jcom.defines.Define.values then begin
if Common.raw_defined ctx.jcom "java_loader_debug" then begin
let sup = jc.csuper :: jc.cinterfaces in
print_endline ("converting " ^ (if List.mem JAbstract jc.cflags then "abstract " else "") ^ JData.path_s jc.cpath ^ " : " ^ (String.concat ", " (List.map s_sig sup)));
end;
Expand Down Expand Up @@ -1262,7 +1262,7 @@ let add_java_lib com name std extern modern =

let before_generate con =
let java_ver = try
int_of_string (PMap.find "java_ver" con.defines.Define.values)
int_of_string (Common.defined_value con Define.JavaVer)
with | Not_found ->
Common.define_value con Define.JavaVer "7";
7
Expand Down
46 changes: 22 additions & 24 deletions src/compiler/haxe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ let error ctx msg p =
message ctx (CMError(msg,p));
ctx.has_error <- true

let reserved_flags = [
"true";"false";"null";"cross";"js";"lua";"neko";"flash";"php";"cpp";"cs";"java";"python";
"swc";"macro";"sys";"static";"utf16";"haxe";"haxe_ver"
]

let reserved_flag_namespaces = ["target"]

let delete_file f = try Sys.remove f with _ -> ()

let expand_env ?(h=None) path =
Expand Down Expand Up @@ -196,7 +189,10 @@ module Initialize = struct
if com.platform <> Cross then failwith "Multiple targets";
Common.init_platform com pf;
com.file <- file;
if (pf = Flash) && file_extension file = "swc" then Common.define com Define.Swc
if (pf = Flash) && file_extension file = "swc" then Common.define com Define.Swc;
(* Set the source header, unless the user has set one already or the platform sets a custom one *)
if not (Common.defined com Define.SourceHeader) && (pf <> Hl) then
Common.define_value com Define.SourceHeader ("Generated by Haxe " ^ s_version_full)

let initialize_target ctx com classes =
let add_std dir =
Expand All @@ -216,7 +212,6 @@ module Initialize = struct
loop l
in
loop Common.flash_versions;
Common.raw_define com "flash";
com.package_rules <- PMap.remove "flash" com.package_rules;
add_std "flash";
"swf"
Expand All @@ -237,7 +232,7 @@ module Initialize = struct
if es_version < 3 || es_version = 4 then (* we don't support ancient and there's no 4th *)
failwith "Invalid -D js-es value";

if es_version >= 5 then Common.raw_define com "js-es5"; (* backward-compatibility *)
if es_version >= 5 then Common.raw_define com "js_es5"; (* backward-compatibility *)

add_std "js";
"js"
Expand Down Expand Up @@ -392,7 +387,8 @@ let setup_common_context ctx com =
Common.define_value com Define.HaxeVer (Printf.sprintf "%.3f" (float_of_int Globals.version /. 1000.));
Common.raw_define com "haxe3";
Common.raw_define com "haxe4";
Common.define_value com Define.Haxe (s_version false);
Common.define_value com Define.Haxe s_version;
Common.raw_define com "true";
Common.define_value com Define.Dce "std";
com.info <- (fun msg p -> message ctx (CMInfo(msg,p)));
com.warning <- (fun msg p -> message ctx (CMWarning(msg,p)));
Expand Down Expand Up @@ -491,7 +487,14 @@ let create_typer_context ctx native_libs =
let com = ctx.com in
ctx.setup();
Common.log com ("Classpath: " ^ (String.concat ";" com.class_path));
Common.log com ("Defines: " ^ (String.concat ";" (PMap.foldi (fun k v acc -> (match v with "1" -> k | _ -> k ^ "=" ^ v) :: acc) com.defines.Define.values [])));
let buffer = Buffer.create 64 in
Buffer.add_string buffer "Defines: ";
PMap.iter (fun k v -> match v with
| "1" -> Printf.bprintf buffer "%s;" k
| _ -> Printf.bprintf buffer "%s=%s;" k v
) com.defines.values;
Buffer.truncate buffer (Buffer.length buffer - 1);
Common.log com (Buffer.contents buffer);
Typecore.type_expr_ref := (fun ?(mode=MGet) ctx e with_type -> Typer.type_expr ~mode ctx e with_type);
List.iter (fun f -> f ()) (List.rev com.callbacks#get_before_typer_create);
(* Native lib pass 1: Register *)
Expand Down Expand Up @@ -688,7 +691,7 @@ let rec process_params create pl =
and init ctx =
let usage = Printf.sprintf
"Haxe Compiler %s - (C)2005-2020 Haxe Foundation\nUsage: haxe%s <target> [options] [hxml files and dot paths...]\n"
(s_version true) (if Sys.os_type = "Win32" then ".exe" else "")
s_version_full (if Sys.os_type = "Win32" then ".exe" else "")
in
let com = ctx.com in
let classes = ref [([],"Std")] in
Expand Down Expand Up @@ -790,18 +793,13 @@ try
),"<class>","select startup class");
("Compilation",["-L";"--library"],["-lib"],Arg.String (fun l ->
cp_libs := l :: !cp_libs;
Common.raw_define com l;
Common.external_define com l;
),"<name[:ver]>","use a haxelib library");
("Compilation",["-D";"--define"],[],Arg.String (fun var ->
let flag = try fst (ExtString.String.split var "=") with _ -> var in
let raise_reserved description =
raise (Arg.Bad (description ^ " and cannot be defined from the command line"))
in
if List.mem flag reserved_flags then raise_reserved (Printf.sprintf "`%s` is a reserved compiler flag" flag);
List.iter (fun ns ->
if ExtString.String.starts_with flag (ns ^ ".") then raise_reserved (Printf.sprintf "`%s` uses the reserved compiler flag namespace `%s.*`" flag ns)
) reserved_flag_namespaces;
Common.raw_define com var;
let flag, value = try let split = ExtString.String.split var "=" in (fst split, Some (snd split)) with _ -> var, None in
match value with
| Some value -> Common.external_define_value com flag value
| None -> Common.external_define com flag;
),"<var[=value]>","define a conditional compilation flag");
("Debug",["-v";"--verbose"],[],Arg.Unit (fun () ->
com.verbose <- true
Expand All @@ -811,7 +809,7 @@ try
com.debug <- true;
),"","add debug information to the compiled code");
("Miscellaneous",["--version"],["-version"],Arg.Unit (fun() ->
com.info (s_version true) null_pos;
com.info s_version_full null_pos;
did_something := true;
),"","print version and exit");
("Miscellaneous", ["-h";"--help"], ["-help"], Arg.Unit (fun () ->
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ let default_flush ctx =

let create_context params =
let ctx = {
com = Common.create version (s_version true) params;
com = Common.create version params;
flush = (fun()->());
setup = (fun()->());
messages = [];
Expand Down
9 changes: 6 additions & 3 deletions src/compiler/serverMessage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ let completion str =

let defines com tabs =
if config.print_defines then begin
let defines = PMap.foldi (fun k v acc -> (k ^ "=" ^ v) :: acc) com.defines.Define.values [] in
print_endline ("Defines " ^ (String.concat "," (List.sort compare defines)))
let buffer = Buffer.create 64 in
Buffer.add_string buffer "Defines ";
PMap.iter (Printf.bprintf buffer "%s=%s,") com.defines.values;
Buffer.truncate buffer (Buffer.length buffer - 1);
print_endline (Buffer.contents buffer)
end

let signature com tabs sign =
Expand Down Expand Up @@ -187,4 +190,4 @@ let set_by_name name value = match name with
| "socketMessage" -> config.print_socket_message <- value;
| "uncaughtError" -> config.print_uncaught_error <- value;
| "newContext" -> config.print_new_context <- value;
| _ -> raise Not_found
| _ -> raise Not_found
Loading

0 comments on commit 0ef19b4

Please sign in to comment.