Commits

camlspotter committed de1512a

Build.ml

  • Participants
  • Parent commits 31a2e75
  • Branches android

Comments (0)

Files changed (4)

 byterun/stdlib\.h$
 byterun/ocamlrun\.target$
 yacc/ocamlyacc\.target$
+config/Makefile$
+
 #load "unix.cma"
+#load "str.cma"
 
 open Printf
 
 
 (******************************************** SETTINGS. PLEASE EDIT THESE!!! *)
 
+(* Android NDK settings *)
+
 (** The directory of the NDK, where you can find ``ndk-buid'' *)
 let ndk_home = sprintf "%s/.share/opt/android-ndk-r8" home
 
-(** The target architecture. Only "arm" is supported for now. *)
+(** The host architecture, one of your Computer. 
+    ex. "linux-x86", "darwin-x86"
+*)
+let host_arch = "linux-x86"
+
+(** The target architecture, one of your Android. 
+    Only "arm" is supported for now. *)
 let target_arch = "arm"
 
-(** The platform revision. "14" is for android-14 for example. *)
-let platform_rev = "14"
+(** The platform version. "14" is for android-14 for example. *)
+let platform_version = "14"
 
-(** The root of android platform, where you can find ``usr'' *)
-let ndk_root = sprintf "%s/platforms/android-%s/arch-%s" ndk_home platform_rev target_arch
+(** The version of GCC in the NDK *)
+let gcc_version = "4.4.3"
+
+(* Installation directory *)
+
+(** Where to install the OCaml cross compilation environment.
+    NOTE: Do not use typical directory such as /usr/bin/, /usr/local/bin, etc.
+    The compiler being installed does NOT produce execs for your Computer,
+    but your Android phone!
+*)
+let prefix = sprintf "%s/.share/prefix/ocaml-android" home
 
 (********************************************************** END OF SETTINGS. *)
 
 module String = struct
   include String
 
-  (* Perl's chop: drop the last char *)
-  let chop = function
-    | "" -> ""
-    | s -> String.sub s 0 (String.length s - 1)
+  let chop_newline s =
+    let len = String.length s in
+    if len > 1 && s.[len-1] = '\n' then
+      if len > 2 && s.[len-2] = '\r' then String.sub s 0 (len-2)
+      else String.sub s 0 (len-1)
+    else s
+
+  let split_by_newline s =
+    let length = String.length s in
+    let rec aux st start_pos pos = 
+      if pos = length then List.rev st else match s.[pos] with
+      | '\r' | '\n' -> 
+          let st = String.sub s start_pos (pos - start_pos) :: st in
+          skip st (pos+1)
+      | _ -> aux st start_pos (pos+1)
+    and skip st pos = 
+      if pos = length then List.rev st else match s.[pos] with
+      | '\r' | '\n' -> skip st (pos+1)
+      | _ -> aux st pos (pos+1)
+    in
+    aux [] 0 0
 end
 
 module Unix = struct
     Buffer.contents buf
 
   (* CR jfuruse: Only works for Unix env. Die Systems with "\r\n"! *)
-  let which cmd = match String.chop (exec (Printf.sprintf "which %s" cmd)) with
+  let which cmd = match String.chop_newline (exec (Printf.sprintf "which %s" cmd)) with
     | "" -> None
     | s -> Some s
 end
     | None -> failwithf "Program %s is not found" s
 end
 
+module At = struct
+  (** Small @var@ replacement language *)
+    
+  let replace_variables tbl =
+    let rex = Str.regexp "@[A-Za-z0-9_]+@" in
+    let replace = 
+      Str.global_substitute rex (fun s ->
+        let matched = Str.matched_string s in
+        let k = String.sub matched 1 (String.length matched - 2) in
+        try List.assoc k tbl with Not_found -> 
+          failwithf "Variable %s is not found" s)
+    in
+    replace
+  
+  let replace_file tbl path outpath =
+    let ic = open_in path in
+    let oc = open_out outpath in
+    let rec loop () = 
+      try 
+        let line = input_line ic in
+        output_string oc (replace_variables tbl line);
+        output_char oc '\n';
+        loop ()
+      with
+      | End_of_file -> close_in ic; close_out oc
+    in
+    loop ()
+end
+
 (* Host tools: ocamlrun and ocamlyacc must exist in the path. *)
 let ocamlrun  = Test.program "ocamlrun"
 let ocamlyacc = Test.program "ocamlyacc"
 let _ = printf "ocamlrun:  %s\n%!" ocamlrun
 let _ = printf "ocamlyacc: %s\n%!" ocamlyacc
 
+(* Android SDK compiler check *)
+
+let cross_tool_dir = 
+  sprintf "%s/toolchains/%s-linux-androideabi-%s/prebuilt/%s" 
+    ndk_home target_arch gcc_version host_arch
+
+let libgcc_dir = 
+  sprintf "%s/toolchains/%s-linux-androideabi-%s/prebuilt/%s/lib/gcc/%s-linux-androideabi/%s" 
+    ndk_home target_arch gcc_version host_arch target_arch gcc_version
+
+let libc_dir = 
+  sprintf "%s/platforms/android-%s/arch-%s/usr/lib"
+    ndk_home platform_version target_arch 
+
+let target_root_dir = 
+  sprintf "%s/platforms/android-%s/arch-%s"
+    ndk_home platform_version target_arch 
+
+let _ = 
+  if not (Sys.file_exists (sprintf "%s/bin/%s-linux-androideabi-gcc" cross_tool_dir target_arch)) then
+    failwithf "gcc is not found at %s/bin" cross_tool_dir
+  else printf "gcc found in %s/bin\n%!" cross_tool_dir;
+  
+  if not (Sys.file_exists (sprintf "%s/libgcc.a" libgcc_dir)) then
+    failwithf "libgcc.a is not found at %s" libgcc_dir
+  else printf "libgcc.a found in %s\n%!" libgcc_dir;
+
+  if not (Sys.file_exists (sprintf "%s/libc.so" libc_dir)) then
+    failwithf "libc.so is not found at %s" libc_dir
+  else printf "libc.so found in %s\n%!" libc_dir;
+
+  if not (Sys.file_exists (sprintf "%s/usr/include/stdlib.h" target_root_dir)) then
+    failwithf "stdlib.h is not found at %s/usr/include" target_root_dir
+  else printf "stdlib.h found in %s/usr/include\n%!" target_root_dir
+
+(* Create config/Makefile *)
+let tbl = [ "prefix"          , prefix
+          ; "cross_tool_dir"  , cross_tool_dir
+          ; "libgcc_dir"      , libgcc_dir
+          ; "target_root_dir" , target_root_dir
+          ]
+
+let _ = At.replace_file tbl "config/Makefile.android.temp" "config/Makefile"
+
 (* Copy stdlib.h *)
 let _ =
-  let path = sprintf "%s/usr/include/stdlib.h" ndk_root in
+  let path = sprintf "%s/usr/include/stdlib.h" target_root_dir in
   printf "Copying NDK stdlib.h: %s\n%!" path;
-  Sys.command (sprintf "cp %s/usr/include/stdlib.h byterun" ndk_root)
+  Sys.command (sprintf "cp %s/usr/include/stdlib.h byterun" target_root_dir)
 
 (* Patch stdlib.h *)
 let _ =

File config/Makefile

-### designated for Android NDK r7
-
-### 1. EDIT BELOW ------------------------------------
-PREFIX=/home/jun/.share/prefix/ocaml-android
-ANDROID_NDK_HOME=/home/jun/.share/opt/android-ndk-r8
-HOST_ARCH=linux-x86
-
-## the directory where gcc stays
-ARM_EABI_GCC=$(ANDROID_NDK_HOME)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(HOST_ARCH)
-## the directory where libgcc.a stays
-ARM_EABI_GCCLIB=$(ARM_EABI_GCC)/lib/gcc/arm-linux-androideabi/4.4.3
-## the directory where /usr/lib/libc.so stays
-ANDROID_PLATFORM=$(ANDROID_NDK_HOME)/platforms/android-14/arch-arm
-
-# The following lines with `ml' prefixes must be configured!!!
-#ml let android_ndk_home = "/home/jun/.share/opt/android-ndk-r8"
-#ml let host_arch = "linux-x86"
-#ml let android_eabi_gcc = Printf.sprintf "%s/toolchains/arm-linux-androideabi-4.4.3/prebuilt/%s" android_ndk_home host_arch
-#ml let mklib out files opts = Printf.sprintf "%s/bin/arm-linux-androideabi-ar rc %s %s %s; %s/bin/arm-linux-androideabi-ranlib %s" android_eabi_gcc out opts files android_eabi_gcc out;;
-
-### 1. EDIT ABOVE ------------------------------------
-
-
-### ANDROID_GCC=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -nostdlib -isystem$(ANDROID_PLATFORM)/usr/include
-ANDROID_GCC=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc
-ANDROID_COMP_OPTS=-isystem$(ANDROID_PLATFORM)/usr/include -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fno-strict-aliasing -finline-limit=64 -Wa,--noexecstack -O0 -g -c -MMD -MP -fno-defer-pop -Wall -D_FILE_OFFSET_BITS=64 -DANDROID -D__BEOS__ -D__ARM_EABI__
-ANDROID_LINK_OPTS=-nostdlib -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc
-
-ANDROID_LIBS=-l:$(ARM_EABI_GCCLIB)/libgcc.a -L$(ANDROID_PLATFORM)/usr/lib -lc -lm -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-rpath-link=$(ANDROID_PLATFORM)/usr/lib
-
-AR=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-ar 
-BYTECC=$(ANDROID_GCC)
-BYTECCCOMPOPTS=$(ANDROID_COMP_OPTS)
-BYTECCLINKOPTS=$(ANDROID_LINK_OPTS)
-BYTECCLIBS=$(ANDROID_LIBS)
-BYTECCRPATH=
-
-NATIVECC=$(ANDROID_GCC)
-NATIVECCCOMPOPTS=$(ANDROID_COMP_OPTS)
-NATIVECCPROFOPTS=$(ANDROID_COMP_OPTS)
-NATIVECCLINKOPTS=$(ANDROID_LINK_OPTS)
-NATIVECCRPATH=
-NATIVECCLIBS=$(ANDROID_LIBS)
-
-PARTIALLD=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-ld -r
-PACKLD=$(PARTIALLD) -dynamic-linker /system/bin/linker -z nocopyreloc -o\ 
-
-MKEXE=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -nostdlib $(ANDROID_LINK_OPTS) $(ANDROID_PLATFORM)/usr/lib/crtbegin_dynamic.o
-MKEXE_EXTRA=$(ANDROID_PLATFORM)/usr/lib/crtend_android.o
-### MKDLL=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -shared -flat_namespace -nostdlib -Wl,-soname,$(TARGET) -Wl,-shared,-Bsymbolic $(OBJ) -Wl,--whole-archive -Wl,--no-whole-archive $(ANDROID_LIBS)
-MKDLL=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -shared -nostdlib -Wl,-shared,-Bsymbolic
-MKDLL_EXTRA=
-MKMAINDLL=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -shared -nostdlib -Wl,-shared,-Bsymbolic
-MKMAINDLL_EXTRA=
-
-RANLIB=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-ranlib
-RANLIBCMD=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-ranlib
-
-### How to build a static library
-MKLIB=$(AR) $(1) $(2); $(ARM_EABI_GCC)/bin/arm-linux-androideabi-ranlib $(1)
-
-ASPPPROFFLAGS=-DPROFILING
-ASPP=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -c
-ASM=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-as
-
-### NATDYNLINK=true
-NATDYNLINK=false
-
-
-
-
-BINDIR=$(PREFIX)/bin
-LIBDIR=$(PREFIX)/lib/ocaml
-STUBLIBDIR=$(LIBDIR)/stublibs
-MANDIR=$(PREFIX)/man
-MANEXT=1
-SHARPBANGSCRIPTS=true
-BNG_ARCH=generic
-BNG_ASM_LEVEL=0
-### PTHREAD_LINK=-cclib -lpthread
-PTHREAD_LINK=
-X11_INCLUDES=-I/usr/X11R6/include
-X11_LINK=-L/usr/X11R6/lib -lX11
-DBM_INCLUDES=
-DBM_LINK=
-TK_DEFS=
-TK_LINK=
-LIBBFD_LINK=
-EXE=
-### SUPPORTS_SHARED_LIBRARIES=true
-SUPPORTS_SHARED_LIBRARIES=false
-SHAREDCCCOMPOPTS=
-MKSHAREDLIBRPATH=
-NATDYNLINKOPTS=
-SYSLIB=-l$(1)
-#ml let syslib x = "-l"^x;;
-
-ARCH=arm
-MODEL=default
-SYSTEM=linux
-
-PROFILING=prof
-DYNLINKOPTS=
-### OTHERLIBRARIES=unix str num dynlink bigarray systhreads threads graph dbm labltk
-OTHERLIBRARIES=unix str num bigarray dynlink
-DEBUGGER=ocamldebugger
-CC_PROFILE=-pg
-### SYSTHREAD_SUPPORT=true
-SYSTHREAD_SUPPORT=false
-
-DLLCCCOMPOPTS=
-IFLEXDIR=
-O=o
-A=a
-SO=so
-EXT_OBJ=.o
-EXT_ASM=.s
-EXT_LIB=.a
-EXT_DLL=.so
-EXTRALIBS=
-CCOMPTYPE=cc
-TOOLCHAIN=cc
-CMXS=cmxs

File config/Makefile.android.temp

+### designated for Android NDK r7
+
+### 1. EDIT BELOW ------------------------------------
+PREFIX=@prefix@
+
+## the directory where bin/gcc stays
+ARM_EABI_GCC=@cross_tool_dir@
+## the directory where libgcc.a stays
+ARM_EABI_GCCLIB=@libgcc_dir@
+## the directory where /usr/lib/libc.so stays
+ANDROID_PLATFORM=@target_root_dir@
+
+# The following lines with `ml' prefixes must be configured!!!
+#ml let mklib out files opts = Printf.sprintf "@cross_tool_dir@/bin/arm-linux-androideabi-ar rc %s %s %s; @cross_tool_dir@/bin/arm-linux-androideabi-ranlib %s" out opts files out;;
+
+### 1. EDIT ABOVE ------------------------------------
+
+
+### ANDROID_GCC=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -nostdlib -isystem$(ANDROID_PLATFORM)/usr/include
+ANDROID_GCC=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc
+ANDROID_COMP_OPTS=-isystem$(ANDROID_PLATFORM)/usr/include -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fno-strict-aliasing -finline-limit=64 -Wa,--noexecstack -O0 -g -c -MMD -MP -fno-defer-pop -Wall -D_FILE_OFFSET_BITS=64 -DANDROID -D__BEOS__ -D__ARM_EABI__
+ANDROID_LINK_OPTS=-nostdlib -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc
+
+ANDROID_LIBS=-l:$(ARM_EABI_GCCLIB)/libgcc.a -L$(ANDROID_PLATFORM)/usr/lib -lc -lm -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-rpath-link=$(ANDROID_PLATFORM)/usr/lib
+
+AR=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-ar 
+BYTECC=$(ANDROID_GCC)
+BYTECCCOMPOPTS=$(ANDROID_COMP_OPTS)
+BYTECCLINKOPTS=$(ANDROID_LINK_OPTS)
+BYTECCLIBS=$(ANDROID_LIBS)
+BYTECCRPATH=
+
+NATIVECC=$(ANDROID_GCC)
+NATIVECCCOMPOPTS=$(ANDROID_COMP_OPTS)
+NATIVECCPROFOPTS=$(ANDROID_COMP_OPTS)
+NATIVECCLINKOPTS=$(ANDROID_LINK_OPTS)
+NATIVECCRPATH=
+NATIVECCLIBS=$(ANDROID_LIBS)
+
+PARTIALLD=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-ld -r
+PACKLD=$(PARTIALLD) -dynamic-linker /system/bin/linker -z nocopyreloc -o\ 
+
+MKEXE=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -nostdlib $(ANDROID_LINK_OPTS) $(ANDROID_PLATFORM)/usr/lib/crtbegin_dynamic.o
+MKEXE_EXTRA=$(ANDROID_PLATFORM)/usr/lib/crtend_android.o
+### MKDLL=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -shared -flat_namespace -nostdlib -Wl,-soname,$(TARGET) -Wl,-shared,-Bsymbolic $(OBJ) -Wl,--whole-archive -Wl,--no-whole-archive $(ANDROID_LIBS)
+MKDLL=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -shared -nostdlib -Wl,-shared,-Bsymbolic
+MKDLL_EXTRA=
+MKMAINDLL=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -shared -nostdlib -Wl,-shared,-Bsymbolic
+MKMAINDLL_EXTRA=
+
+RANLIB=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-ranlib
+RANLIBCMD=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-ranlib
+
+### How to build a static library
+MKLIB=$(AR) $(1) $(2); $(ARM_EABI_GCC)/bin/arm-linux-androideabi-ranlib $(1)
+
+ASPPPROFFLAGS=-DPROFILING
+ASPP=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-gcc -c
+ASM=$(ARM_EABI_GCC)/bin/arm-linux-androideabi-as
+
+### NATDYNLINK=true
+NATDYNLINK=false
+
+
+
+
+BINDIR=$(PREFIX)/bin
+LIBDIR=$(PREFIX)/lib/ocaml
+STUBLIBDIR=$(LIBDIR)/stublibs
+MANDIR=$(PREFIX)/man
+MANEXT=1
+SHARPBANGSCRIPTS=true
+BNG_ARCH=generic
+BNG_ASM_LEVEL=0
+### PTHREAD_LINK=-cclib -lpthread
+PTHREAD_LINK=
+X11_INCLUDES=-I/usr/X11R6/include
+X11_LINK=-L/usr/X11R6/lib -lX11
+DBM_INCLUDES=
+DBM_LINK=
+TK_DEFS=
+TK_LINK=
+LIBBFD_LINK=
+EXE=
+### SUPPORTS_SHARED_LIBRARIES=true
+SUPPORTS_SHARED_LIBRARIES=false
+SHAREDCCCOMPOPTS=
+MKSHAREDLIBRPATH=
+NATDYNLINKOPTS=
+SYSLIB=-l$(1)
+#ml let syslib x = "-l"^x;;
+
+ARCH=arm
+MODEL=default
+SYSTEM=linux
+
+PROFILING=prof
+DYNLINKOPTS=
+### OTHERLIBRARIES=unix str num dynlink bigarray systhreads threads graph dbm labltk
+OTHERLIBRARIES=unix str num bigarray dynlink
+DEBUGGER=ocamldebugger
+CC_PROFILE=-pg
+### SYSTHREAD_SUPPORT=true
+SYSTHREAD_SUPPORT=false
+
+DLLCCCOMPOPTS=
+IFLEXDIR=
+O=o
+A=a
+SO=so
+EXT_OBJ=.o
+EXT_ASM=.s
+EXT_LIB=.a
+EXT_DLL=.so
+EXTRALIBS=
+CCOMPTYPE=cc
+TOOLCHAIN=cc
+CMXS=cmxs