apply plugin: 'com.github.hauner.jarTest'
apply plugin: 'cpp'
apply plugin: 'eclipse'

eclipse.project.natures 'org.eclipse.pde.PluginNature'

sourceSets.test.java.srcDirs = ['harness-src/java']

dependencies {
	testCompile(
		'junit:junit:4.11',
		project(':com.ibm.wala.cast'),
		project(':com.ibm.wala.core'),
		project(':com.ibm.wala.util'),
		project(configuration: 'testArchives', path: ':com.ibm.wala.core.tests'),
		)
}

// TODO: build following targets for "test"-only, not "main"

def currentJvm = org.gradle.internal.jvm.Jvm.current()

task generateJniHeaders(type: Exec, dependsOn: [testClasses, ':com.ibm.wala.cast:classes']) {
	// classes for which we need headers
	def nativePackage = 'com.ibm.wala.cast.test'
	def nativeClasses = ['TestNativeTranslator']
	def qualifiedClasses = nativeClasses.collect { "$nativePackage.$it" }

	// inputs and outputs, suitable for use with rule-based model configuration
	ext {
		sourceDir = null
		headerDir = "$buildDir/include"
	}
	inputs.files files(qualifiedClasses.collect { "$buildDir/${it.replace('.', '/')}.class" })
	outputs.files files(qualifiedClasses.collect { "$headerDir/${it.replace('.', '_')}.h" })

	// javah command to generate headers
	def sourceSets = [sourceSets.test, project(':com.ibm.wala.cast').sourceSets.main]
	def classesDirs = sourceSets.inject(files()) { acc, val -> acc.plus(val.output.classesDirs) }
	def classpath = classesDirs.asPath
	def javah = currentJvm.getExecutable('javah')
	commandLine(javah, '-d', "$headerDir", '-classpath', classpath)
	args(qualifiedClasses)
}

model {
	components {
		xlator_test(NativeLibrarySpec) {
			sources.cpp {
				source {
					srcDirs = ['harness-src/c']
					include 'smoke.cpp'
				}
				generatedBy generateJniHeaders
				['cast', 'jdk'].each {
					lib project: ':com.ibm.wala.cast', library: it
				}
			}
			binaries {
				withType(StaticLibraryBinarySpec) {
					buildable = false
				}
				withType(SharedLibraryBinarySpec) {
					switch ("$targetPlatform.operatingSystem.name/$targetPlatform.architecture.name") {
						case 'linux/x86-64':
							// TODO: compute path on following line from 'cast' library properties somehow
							linker.args '-Wl,-rpath', "${project(':com.ibm.wala.cast').buildDir}/libs/cast/shared"
							// TODO: compute path on following line from 'jdk' library properties somehow
							linker.args '-Wl,-rpath', "${currentJvm.javaHome}/jre/lib/amd64/server"
					}
				}
			}
		}
		smoke_main(NativeExecutableSpec) {
			sources.cpp {
				source {
					srcDirs = ['harness-src/c']
					include 'smoke_main.cpp'
				}
				['cast', 'jdk'].each {
					lib project: ':com.ibm.wala.cast', library: it
				}
			}
			binaries.all {
					switch ("$targetPlatform.operatingSystem.name/$targetPlatform.architecture.name") {
						case 'linux/x86-64':
							// TODO: compute path on following line from 'cast' library properties somehow
							linker.args '-Wl,-rpath', "${project(':com.ibm.wala.cast').buildDir}/libs/cast/shared"
							// TODO: compute path on following line from 'jdk' library properties somehow
							linker.args '-Wl,-rpath', "${currentJvm.javaHome}/jre/lib/amd64/server"
					}
			}
		}
	}

	tasks.test {
		def lib = linkXlator_testSharedLibrary
		dependsOn lib
		systemProperty 'java.library.path', lib.destinationDir
	}
}

// https://github.com/liblit/WALA/issues/3
if (System.getProperty('os.name') == 'Mac OS X')
	test.exclude '**/TestNativeTranslator.class'
