Sfoglia il codice sorgente

[netlib] Added -net-std lookup, and net_target and net_ver defines

Caue Waneck 12 anni fa
parent
commit
f8bcbe7d2d
3 ha cambiato i file con 89 aggiunte e 4 eliminazioni
  1. 6 0
      common.ml
  2. 66 3
      gencs.ml
  3. 17 1
      main.ml

+ 6 - 0
common.ml

@@ -147,6 +147,7 @@ type context = {
 	mutable swf_libs : (string * (unit -> Swf.swf) * (unit -> ((string list * string),As3hl.hl_class) Hashtbl.t)) list;
 	mutable java_libs : (string * bool * (unit -> unit) * (unit -> (path list)) * (path -> ((JData.jclass * string * string) option))) list; (* (path,std,close,all_files,lookup) *)
 	mutable net_libs : (string * bool * (unit -> path list) * (path -> IlData.ilclass option)) list; (* (path,std,all_files,lookup) *)
+	mutable net_std : string list;
 	net_path_map : (path,string list * string list * string) Hashtbl.t;
 	mutable js_gen : (unit -> unit) option;
 	(* typing *)
@@ -192,6 +193,8 @@ module Define = struct
 		| NekoSource
 		| NekoV1
 		| NetworkSandbox
+		| NetVer
+		| NetTarget
 		| NoCompilation
 		| NoCOpt
 		| NoFlashOverride
@@ -252,6 +255,8 @@ module Define = struct
 		| JsFlatten -> ("js_flatten","Generate classes to use fewer object property lookups")
 		| Macro -> ("macro","Defined when we compile code in the macro context")
 		| MacroTimes -> ("macro_times","Display per-macro timing when used with --times")
+		| NetVer -> ("net_ver", "<version:20-45> Sets the .NET version to be targeted")
+		| NetTarget -> ("net_target", "<name> Sets the .NET target. Defaults to \"net\". xbox, micro (Micro Framework), compact (Compact Framework) are some valid values")
 		| NekoSource -> ("neko_source","Output neko source instead of bytecode")
 		| NekoV1 -> ("neko_v1","Keep Neko 1.x compatibility")
 		| NetworkSandbox -> ("network-sandbox","Use local network sandbox instead of local file access one")
@@ -652,6 +657,7 @@ let create v args =
 		swf_libs = [];
 		java_libs = [];
 		net_libs = [];
+		net_std = [];
 		net_path_map = Hashtbl.create 0;
 		neko_libs = [];
 		php_prefix = None;

+ 66 - 3
gencs.ml

@@ -2474,9 +2474,6 @@ let configure gen =
 
 (* end of configure function *)
 
-let before_generate con =
-  ()
-
 let generate con =
   (try
     let gen = new_ctx con in
@@ -3134,6 +3131,9 @@ let normalize_ilcls ctx cls =
 		cprops = List.map (function | (IlProp f,_,_,_) -> f | _ -> assert false) props;
 	}
 
+let add_net_std com file =
+	com.net_std <- file :: com.net_std
+
 let add_net_lib com file std =
 	let ilctx = ref None in
 	let netpath_to_hx = netpath_to_hx std in
@@ -3215,3 +3215,66 @@ let add_net_lib com file std =
   com.load_extern_type <- com.load_extern_type @ [build];
   com.net_libs <- (file, std, all_files, lookup) :: com.net_libs
 
+let before_generate com =
+	(* net version *)
+	let net_ver = try
+			int_of_string (PMap.find "net_ver" com.defines)
+		with | Not_found ->
+			Common.define_value com Define.NetVer "20";
+			20
+	in
+	if net_ver < 20 then
+		failwith (
+			".NET version is defined to target .NET "
+			^ string_of_int net_ver
+			^ ", but the compiler can only output code to versions equal or superior to .NET 2.0 (defined as 20)"
+		);
+	let rec loop = function
+		| v :: acc when v <= net_ver ->
+			Common.raw_define com ("NET_" ^ string_of_int v);
+			loop acc
+		| _ -> ()
+	in
+	loop [20;21;30;35;40;45];
+
+	(* net target *)
+	let net_target = try
+			String.lowercase (PMap.find "net_target" com.defines)
+		with | Not_found ->
+			"net"
+	in
+	Common.define_value com Define.NetTarget net_target;
+	Common.raw_define com net_target;
+
+	(* std dirs *)
+	let stds = match com.net_std with
+		| [] -> ["netlib"]
+		| s -> s
+	in
+	(* look for all dirs that have the digraph NET_TARGET-NET_VER *)
+	let digraph = net_target ^ "-" ^ string_of_int net_ver in
+	let matched = ref [] in
+	List.iter (fun f -> try
+		let f = Common.find_file com (f ^ "/" ^ digraph) in
+		matched := (f, Unix.opendir f) :: !matched
+	with | _ -> ()) stds;
+
+	if !matched = [] then failwith (
+		"No .NET std lib directory with the pattern '" ^ digraph ^ "' was found in the -net-std search path. " ^
+		"Try updating the hxcs lib to the latest version, or specifying another -net-std path.");
+	List.iter (fun (path,f) ->
+		let rec loop () =
+			try
+				let f = Unix.readdir f in
+				let finsens = String.lowercase f in
+				if String.ends_with finsens ".dll" then
+					add_net_lib com (path ^ "/" ^ f) true;
+				loop()
+			with | End_of_file ->
+				Unix.closedir f
+		in
+		loop()
+	) !matched;
+
+	(* now force all libraries to initialize *)
+	List.iter (function (_,_,_,lookup) -> ignore (lookup ([],""))) com.net_libs

+ 17 - 1
main.ml

@@ -260,6 +260,18 @@ let rec read_type_path com p =
       loop path p
     ) (all_files())
   ) com.java_libs;
+  List.iter (fun (path,std,all_files,lookup) ->
+    List.iter (fun (path, name) ->
+      if path = p then classes := name :: !classes else
+      let rec loop p1 p2 =
+        match p1, p2 with
+        | [], _ -> ()
+        | x :: _, [] -> packages := x :: !packages
+        | a :: p1, b :: p2 -> if a = b then loop p1 p2
+      in
+      loop path p
+    ) (all_files())
+  ) com.net_libs;
 	unique !packages, unique !classes
 
 let delete_file f = try Sys.remove f with _ -> ()
@@ -865,6 +877,7 @@ try
 			set_platform Cpp dir;
 		),"<directory> : generate C++ code into target directory");
  		("-cs",Arg.String (fun dir ->
+			cp_libs := "hxcs" :: !cp_libs;
 			set_platform Cs dir;
 		),"<directory> : generate C# code into target directory");
 		("-java",Arg.String (fun dir ->
@@ -932,8 +945,11 @@ try
 			Genjava.add_java_lib com file false
 		),"<file> : add an external JAR or class directory library");
 		("-net-lib",Arg.String (fun file ->
-			Gencs.add_net_lib com file true
+			Gencs.add_net_lib com file false
 		),"<file> : add an external .NET DLL file");
+		("-net-std",Arg.String (fun file ->
+			Gencs.add_net_std com file
+		),"<file> : add a root std .NET DLL search path");
 		("-x", Arg.String (fun file ->
 			let neko_file = file ^ ".n" in
 			set_platform Neko neko_file;