Home | History | Annotate | Download | only in jnigen
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.jnigen;
     18 
     19 /** Defines the configuration for building a native shared library for a specific platform. Used with {@link AntScriptGenerator} to
     20  * create Ant build files that invoke the compiler toolchain to create the shared libraries. */
     21 public class BuildTarget {
     22 	/** The target operating system of a build target. */
     23 	public enum TargetOs {
     24 		Windows, Linux, MacOsX, Android, IOS
     25 	}
     26 
     27 	/** the target operating system **/
     28 	public BuildTarget.TargetOs os;
     29 	/** whether this is a 64-bit build, not used for Android **/
     30 	public boolean is64Bit;
     31 	/** the C files and directories to be included in the build, accepts Ant path format, must not be null **/
     32 	public String[] cIncludes;
     33 	/** the C files and directories to be excluded from the build, accepts Ant path format, must not be null **/
     34 	public String[] cExcludes;
     35 	/** the C++ files and directories to be included in the build, accepts Ant path format, must not be null **/
     36 	public String[] cppIncludes;
     37 	/** the C++ files and directories to be excluded from the build, accepts Ant path format, must not be null **/
     38 	public String[] cppExcludes;
     39 	/** the directories containing headers for the build, must not be null **/
     40 	public String[] headerDirs;
     41 	/** prefix for the compiler (g++, gcc), useful for cross compilation, must not be null **/
     42 	public String compilerPrefix;
     43 	/** the flags passed to the C compiler, must not be null **/
     44 	public String cFlags;
     45 	/** the flags passed to the C++ compiler, must not be null **/
     46 	public String cppFlags;
     47 	/** the flags passed to the linker, must not be null **/
     48 	public String linkerFlags;
     49 	/** the name of the generated build file for this target, defaults to "build-${target}(64)?.xml", must not be null **/
     50 	public String buildFileName;
     51 	/** whether to exclude this build target from the master build file, useful for debugging **/
     52 	public boolean excludeFromMasterBuildFile = false;
     53 	/** Ant XML executed in a target before compilation **/
     54 	public String preCompileTask;
     55 	/** Ant Xml executed in a target after compilation **/
     56 	public String postCompileTask;
     57 	/** the libraries to be linked to the output, specify via e.g. -ldinput -ldxguid etc. **/
     58 	public String libraries;
     59 	/** The name used for folders for this specific target. Defaults to "${target}(64)" **/
     60 	public String osFileName;
     61 	/** The name used for the library file. This is a full file name, including file extension. Default is platform specific.
     62 	 *  E.g. "lib{sharedLibName}64.so" **/
     63 	public String libName;
     64 
     65 	/** Creates a new build target. See members of this class for a description of the parameters. */
     66 	public BuildTarget (BuildTarget.TargetOs targetType, boolean is64Bit, String[] cIncludes, String[] cExcludes,
     67 		String[] cppIncludes, String[] cppExcludes, String[] headerDirs, String compilerPrefix, String cFlags, String cppFlags,
     68 		String linkerFlags) {
     69 		if (targetType == null) throw new IllegalArgumentException("targetType must not be null");
     70 		if (cIncludes == null) cIncludes = new String[0];
     71 		if (cExcludes == null) cExcludes = new String[0];
     72 		if (cppIncludes == null) cppIncludes = new String[0];
     73 		if (cppExcludes == null) cppExcludes = new String[0];
     74 		if (headerDirs == null) headerDirs = new String[0];
     75 		if (compilerPrefix == null) compilerPrefix = "";
     76 		if (cFlags == null) cFlags = "";
     77 		if (cppFlags == null) cppFlags = "";
     78 		if (linkerFlags == null) linkerFlags = "";
     79 
     80 		this.os = targetType;
     81 		this.is64Bit = is64Bit;
     82 		this.cIncludes = cIncludes;
     83 		this.cExcludes = cExcludes;
     84 		this.cppIncludes = cppIncludes;
     85 		this.cppExcludes = cppExcludes;
     86 		this.headerDirs = headerDirs;
     87 		this.compilerPrefix = compilerPrefix;
     88 		this.cFlags = cFlags;
     89 		this.cppFlags = cppFlags;
     90 		this.linkerFlags = linkerFlags;
     91 		this.libraries = "";
     92 	}
     93 
     94 	/** Creates a new default BuildTarget for the given OS, using common default values. */
     95 	public static BuildTarget newDefaultTarget (BuildTarget.TargetOs type, boolean is64Bit) {
     96 		if (type == TargetOs.Windows && !is64Bit) {
     97 			// Windows 32-Bit
     98 			return new BuildTarget(TargetOs.Windows, false, new String[] {"**/*.c"}, new String[0], new String[] {"**/*.cpp"},
     99 				new String[0], new String[0], "i686-w64-mingw32-", "-c -Wall -O2 -mfpmath=sse -msse2 -fmessage-length=0 -m32",
    100 				"-c -Wall -O2 -mfpmath=sse -msse2 -fmessage-length=0 -m32",
    101 				"-Wl,--kill-at -shared -m32 -static -static-libgcc -static-libstdc++");
    102 		}
    103 
    104 		if (type == TargetOs.Windows && is64Bit) {
    105 			// Windows 64-Bit
    106 			return new BuildTarget(TargetOs.Windows, true, new String[] {"**/*.c"}, new String[0], new String[] {"**/*.cpp"},
    107 				new String[0], new String[0], "x86_64-w64-mingw32-", "-c -Wall -O2 -mfpmath=sse -msse2 -fmessage-length=0 -m64",
    108 				"-c -Wall -O2 -mfpmath=sse -msse2 -fmessage-length=0 -m64",
    109 				"-Wl,--kill-at -shared -static -static-libgcc -static-libstdc++ -m64");
    110 		}
    111 
    112 		if (type == TargetOs.Linux && !is64Bit) {
    113 			// Linux 32-Bit
    114 			return new BuildTarget(TargetOs.Linux, false, new String[] {"**/*.c"}, new String[0], new String[] {"**/*.cpp"},
    115 				new String[0], new String[0], "", "-c -Wall -O2 -mfpmath=sse -msse -fmessage-length=0 -m32 -fPIC",
    116 				"-c -Wall -O2 -mfpmath=sse -msse -fmessage-length=0 -m32 -fPIC", "-shared -m32");
    117 		}
    118 
    119 		if (type == TargetOs.Linux && is64Bit) {
    120 			// Linux 64-Bit
    121 			return new BuildTarget(TargetOs.Linux, true, new String[] {"**/*.c"}, new String[0], new String[] {"**/*.cpp"},
    122 				new String[0], new String[0], "", "-c -Wall -O2 -mfpmath=sse -msse -fmessage-length=0 -m64 -fPIC",
    123 				"-c -Wall -O2 -mfpmath=sse -msse -fmessage-length=0 -m64 -fPIC", "-shared -m64 -Wl,-wrap,memcpy");
    124 		}
    125 
    126 		if (type == TargetOs.MacOsX && !is64Bit) {
    127 			// Mac OS X x86 & x86_64
    128 			BuildTarget mac = new BuildTarget(TargetOs.MacOsX, false, new String[] {"**/*.c"}, new String[0],
    129 				new String[] {"**/*.cpp"}, new String[0], new String[0], "",
    130 				"-c -Wall -O2 -arch i386 -DFIXED_POINT -fmessage-length=0 -fPIC -mmacosx-version-min=10.5",
    131 				"-c -Wall -O2 -arch i386 -DFIXED_POINT -fmessage-length=0 -fPIC -mmacosx-version-min=10.5",
    132 				"-shared -arch i386 -mmacosx-version-min=10.5");
    133 			return mac;
    134 		}
    135 
    136 		if (type == TargetOs.MacOsX && is64Bit) {
    137 			// Mac OS X x86 & x86_64
    138 			BuildTarget mac = new BuildTarget(TargetOs.MacOsX, true, new String[] {"**/*.c"}, new String[0],
    139 				new String[] {"**/*.cpp"}, new String[0], new String[0], "",
    140 				"-c -Wall -O2 -arch x86_64 -DFIXED_POINT -fmessage-length=0 -fPIC -mmacosx-version-min=10.5",
    141 				"-c -Wall -O2 -arch x86_64 -DFIXED_POINT -fmessage-length=0 -fPIC -mmacosx-version-min=10.5",
    142 				"-shared -arch x86_64 -mmacosx-version-min=10.5");
    143 			return mac;
    144 		}
    145 
    146 		if (type == TargetOs.Android) {
    147 			BuildTarget android = new BuildTarget(TargetOs.Android, false, new String[] {"**/*.c"}, new String[0],
    148 				new String[] {"**/*.cpp"}, new String[0], new String[0], "", "-O2 -Wall -D__ANDROID__", "-O2 -Wall -D__ANDROID__",
    149 				"-lm");
    150 			return android;
    151 		}
    152 
    153 		if(type == TargetOs.IOS) {
    154 			// iOS, 386 simulator and armv7a, compiled to fat static lib
    155 			BuildTarget ios = new BuildTarget(TargetOs.IOS, false, new String[] {"**/*.c"}, new String[0],
    156 				new String[] {"**/*.cpp"}, new String[0], new String[0], "",
    157 				"-c -Wall -O2",
    158 				"-c -Wall -O2",
    159 				"rcs");
    160 			return ios;
    161 		}
    162 
    163 		throw new RuntimeException("Unknown target type");
    164 	}
    165 }
    166