build.xml 36.3 KB
Newer Older
1 2 3 4 5
<project
	name="disl" default="build" basedir="."
	xmlns:ivy="antlib:org.apache.ivy.ant"
	xmlns:ac="antlib:net.sf.ant-contrib"
>
6

7
	<property name="build.sysclasspath" value="ignore"/>
8

9
	<property file="disl.version.properties"/>
10

11 12
	<property file="dependencies.local.properties"/>
	<property file="dependencies.properties"/>
13

14 15
	<property file="build.local.properties"/>
	<property file="build.properties"/>
16

17 18
	<property file="common.properties" />

19

20 21 22 23
	<!-- ================================================================== -->
	<!-- IVY SETUP & TASKS                                                  -->
	<!-- ================================================================== -->

24
	<!--
25
		Downloads the Ivy dependency manager from the Maven central repository.
26
	-->
27
	<available file="${build.ivy.path}" property="build.ivy.downloaded"/>
28
	<target name="-download-ivy" unless="build.ivy.downloaded">
29
		<mkdir dir="${lib.ant.dir}"/>
30 31
		<get src="${build.ivy.url}" dest="${build.ivy.path}" usetimestamp="true"/>
	</target>
32 33 34


	<!--
35
		Registers Ivy tasks within Ant.
36
	-->
37
	<target name="-setup-ivy" depends="-download-ivy">
38
		<taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpath="${build.ivy.path}"/>
39
	</target>
40

41

42 43 44 45 46 47
	<!--
		Downloads dependencies for compilation. This target should allow
		unconditional execution so that the download can be always forced.
	-->
	<target name="download-deps" depends="-setup-ivy" description="Downloads dependencies for compilation.">
		<ivy:resolve conf="ant,disl,test" file="${build.ivy.file}"/>
48
		<ivy:retrieve pattern="${lib.dir}/[conf]/[artifact].[ext]"/>
49
	</target>
František Haas's avatar
František Haas committed
50

51

52 53 54 55 56 57 58
	<!--
		Downloads dependency sources (for IDE integration).
	-->
	<target name="download-sources" depends="download-deps" description="Downloads dependency sources (for IDE integration).">
		<ivy:resolve conf="devel" file="${build.ivy.file}"/>
		<ivy:retrieve type="source" pattern="${lib.dir}/[conf]/[artifact]-[type].[ext]"/>
	</target>
59 60


61 62 63
	<!-- ================================================================== -->
	<!-- ARTIFACT LIST                                                      -->
	<!-- ================================================================== -->
64

65
	<!--
66
		Downloads dependencies and stores the list of retrieved artifacts
67
		so that their presence can be checked in subsequent invocations
68
		without Ivy. This target should allow unconditional execution so
69 70 71 72
		that the update of the artifact list can be always forced.
	-->
	<target name="update-artifact-list" depends="download-deps" description="Updates the list of expected dependency artifacts.">
		<collect-artifacts id="build.artifacts"/>
73

74 75 76 77 78 79
		<local name="artifacts.text"/>
		<path-to-text
			property="artifacts.text" refid="build.artifacts"
			mapfrom="${basedir}${file.separator}${lib.dir}"
			mapto="${lib.dir}"
		/>
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		<echo file="${build.artifacts.list}">${artifacts.text}</echo>
	</target>


	<!--
		Determines whether the artifact list is up-to-date and sets the value
		of the 'build.artifacts.list.uptodate' property accordingly. The list
		is considered up-to-date if it contains at least one non-empty line
		and is newer than both the files describing the dependency properties.
	-->
	<target name="-check-artifact-list">
		<condition property="build.artifacts.list.uptodate"><and>
			<resourcecount when="greater" count="0">
				<tokens><concat>
					<filterchain>
						<tokenfilter><ignoreblank/></tokenfilter>
					</filterchain>
					<file file="${build.artifacts.list}"/>
				</concat></tokens>
			</resourcecount>

			<uptodate targetfile="${build.artifacts.list}">
103
				<srcfiles dir="${basedir}" includes="dependencies*.properties"/>
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
			</uptodate>
		</and></condition>
	</target>


	<!--
		Creates artifact list if it is missing or not up-to-date. This will
		trigger an initial download of artifacts so that the list is build
		from the downloaded artifacts.
	-->
	<target name="-create-artifact-list" depends="-check-artifact-list" unless="build.artifacts.list.uptodate">
		<antcall target="update-artifact-list"/>
	</target>


	<!--
		Collects downloaded artifacts into a fileset.
	-->
	<macrodef name="collect-artifacts">
		<attribute name="id"/>
		<sequential>
			<fileset dir="${basedir}" id="@{id}">
				<include name="${lib.ant.dir}/*.jar"/>
				<include name="${lib.disl.dir}/*.jar"/>
				<include name="${lib.test.dir}/*.jar"/>
			</fileset>
		</sequential>
	</macrodef>


	<!--
		Converts a path-like entity to text, one entry per line.
		Optionally performs mapping from one path prefix to another.
	-->
	<macrodef name="path-to-text">
		<attribute name="property"/>
		<attribute name="refid"/>
		<attribute name="mapfrom" default=""/>
		<attribute name="mapto" default=""/>
		<sequential>
			<pathconvert pathsep="${line.separator}" property="@{property}" refid="@{refid}">
				<map from="@{mapfrom}" to="@{mapto}"/>
			</pathconvert>
		</sequential>
	</macrodef>


	<!-- ================================================================== -->
	<!-- DEPENDENCY CHECKS                                                  -->
	<!-- ================================================================== -->

	<!--
		Determines if any of the expected artifacts are missing and sets
		the value of the 'build.artifacts.missing' property accordingly.
158
		This may trigger creating the list of expected artifacts after an
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		initial download of dependencies.
	-->
	<target name="-check-artifacts" depends="-create-artifact-list">
		<find-missing-files file="${build.artifacts.list}" id="build.artifacts.missing.list"/>

		<condition property="build.artifacts.missing">
			<not><resourcecount refid="build.artifacts.missing.list" count="0"/></not>
		</condition>
	</target>


	<!--
		Download dependency artifacts if they are missing.
	-->
	<target name="-download-artifacts" depends="-check-artifacts" if="build.artifacts.missing">
174
		<antcall target="download-deps"/>
175 176 177 178 179 180 181 182 183 184 185 186 187 188
	</target>


	<!--
		Checks that none of the required artifacts are missing. This may
		trigger an initial download of dependency artifacts, but generally
		they will checked against a list of expected artifacts. If any of
		the artifacts are missing at this point, the build will fail.
	-->
	<target name="-check-deps" depends="-download-artifacts">
		<find-missing-files file="${build.artifacts.list}" id="build.artifacts.really.missing.list"/>

		<!-- Sanitize the path names of potentially missing artifacts. -->
		<local name="missing.text"/>
189
		<path-to-text
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			property="missing.text" refid="build.artifacts.really.missing.list"
			mapfrom="${basedir}${file.separator}${lib.dir}"
			mapto="${lib.dir}"
		/>

		<!-- Fail if there are any missing artifacts. -->
		<fail message="missing artifacts${line.separator}${missing.text}${line.separator}Try running 'ant download-deps' to re-download dependencies.">
			<condition>
				<not><resourcecount refid="build.artifacts.really.missing.list" count="0"/></not>
			</condition>
		</fail>
	</target>


	<!--
		Loads a list of files and selects those that are missing.
	-->
	<macrodef name="find-missing-files">
		<attribute name="file"/>
		<attribute name="id"/>
		<sequential>
			<restrict id="@{id}">
				<resourcelist>
					<file file="@{file}"/>
				</resourcelist>
				<not><exists/></not>
			</restrict>
		</sequential>
	</macrodef>


	<!-- ================================================================== -->
	<!-- INITIALIZATION                                                     -->
	<!-- ================================================================== -->

	<!--
		Setup Ant extensions and define class paths involving both the
		downloaded artifacts and project classes.
	-->
	<target name="-init" depends="-check-deps">
		<path id="build.ant.classpath">
231
			<fileset dir="${lib.ant.dir}" includes="*.jar"/>
232 233
		</path>

234
		<taskdef resource="net/sf/antcontrib/antlib.xml" uri="antlib:net.sf.ant-contrib" classpathref="build.ant.classpath"/>
235 236 237

		<!-- Dependency class paths -->
		<path id="asm.classpath">
238
			<fileset dir="${lib.disl.dir}" includes="asm*.jar" id="asm.fileset"/>
239 240 241
		</path>

		<!-- Get ASM libs from ASM class path. -->
242
		<pathconvert pathsep=" " property="asm.libs" refid="asm.classpath">
243 244 245 246
			<mapper type="flatten"/>
		</pathconvert>

		<path id="log4j.classpath">
247
			<fileset dir="${lib.disl.dir}" includes="log4j*.jar"/>
248 249 250
		</path>

		<path id="junit.classpath">
251
			<fileset dir="${lib.test.dir}" includes="*.jar"/>
252 253
		</path>

254 255 256 257 258 259 260 261 262
		<path id="protobuf.classpath">
			<fileset dir="${lib.disl.dir}" includes="protobuf*.jar"/>
		</path>

		<!-- Get ProtoBuf Java libs from ProtoBuf class path. -->
		<pathconvert pathsep=" " property="protobuf.libs" refid="protobuf.classpath">
			<mapper type="flatten"/>
		</pathconvert>

263 264
		<!-- Project class paths -->
		<path id="util.classpath">
265
			<pathelement location="${build.util}"/>
266 267 268
		</path>

		<path id="disl.classpath">
269
			<pathelement location="${src.disl.prefix}/${build.disl}"/>
270 271
		</path>

272 273 274 275
		<path id="disl-server.classpath">
			<pathelement location="${src.disl-server.prefix}/${build.disl-server}"/>
		</path>

276 277 278 279
		<path id="shvm-server.classpath">
			<pathelement location="${src.shvm-server.prefix}/${build.shvm-server}"/>
		</path>

280
		<path id="disl.bypass.classpath">
281
			<pathelement location="${build.disl.bypass}/dynamic"/>
282 283 284
		</path>

		<path id="shvm.classpath">
285
			<pathelement location="${src.shvm.prefix}/${build.shvm}"/>
286
			<pathelement location="${build.shvm.dispatch}"/>
287 288 289 290 291 292 293 294 295 296
		</path>
	</target>


	<!--
		Determines the names of native agent libraries.
		Mac OS X (Darwin) is considered a Unix family.
	-->
	<target name="-determine-lib-names" depends="-init">
		<ac:if>
297
			<os family="windows"/>
298
			<ac:then>
299 300
				<property name="dl.prefix" value=""/>
				<property name="dl.suffix" value=".dll"/>
301
				<property name="disl.agent.os" value="windows"/>
302 303
			</ac:then><ac:else>
				<ac:if>
304
					<os family="unix"/>
305
					<ac:then>
306
						<property name="dl.prefix" value="lib"/>
307
						<ac:if>
308
							<os name="Mac OS X"/>
309
							<ac:then>
310
								<property name="dl.suffix" value=".jnilib"/>
311
								<property name="disl.agent.os" value="macosx"/>
312
							</ac:then><ac:else>
313
								<property name="dl.suffix" value=".so"/>
314
								<property name="disl.agent.os" value="linux"/>
315 316 317
							</ac:else>
						</ac:if>
					</ac:then><ac:else>
318
						<fail message="Unsupported operating system: ${os.name}"/>
319 320 321 322
					</ac:else>
				</ac:if>
			</ac:else>
		</ac:if>
323

324 325 326 327 328 329
		<ac:switch value="${os.arch}" caseinsensitive="true">
			<case value="x86_64"><property name="disl.agent.arch" value="x86_64"/></case>
			<case value="amd64"><property name="disl.agent.arch" value="x86_64"/></case>
			<default><property name="disl.agent.arch" value="x86"/></default>
		</ac:switch>

330 331
		<property name="disl.agent.lib" value="${dl.prefix}${disl.agent.lib.base}${dl.suffix}"/>
		<property name="shvm.agent.lib" value="${dl.prefix}${shvm.agent.lib.base}${dl.suffix}"/>
332 333 334
	</target>


335 336 337 338 339 340
	<!--
		Looks for a class file or a source file with a given name, and
		sets the value of a given property to the fully qualified
		class name when a matching class (or source) file is found.
	-->
	<macrodef name="find-class">
341 342 343 344 345
		<attribute name="dir"/>
		<attribute name="name"/>
		<attribute name="property"/>
		<attribute name="suffix" default=".class"/>
		<attribute name="setonempty" default="true"/>
346 347
		<sequential>
			<pathconvert property="@{property}" setonempty="@{setonempty}">
348 349
				<fileset dir="@{dir}" includes="**/@{name}@{suffix}"/>
				<mapper type="package" from="${basedir}/@{dir}/*@{suffix}" to="*"/>
350 351 352 353 354
			</pathconvert>
		</sequential>
	</macrodef>


355 356
	<!-- Build tools -->

357
	<target name="compile-tools" depends="-init">
358
		<mkdir dir="${build.tools}"/>
359 360
		<javac destdir="${build.tools}" srcdir="${src.tools}">
			<classpath>
361 362
				<path refid="asm.classpath"/>
				<path location="${ant.core.lib}"/>
363 364 365 366 367
			</classpath>
		</javac>
	</target>


368 369
	<!-- DiSL bypass -->

370 371 372 373 374 375 376 377
	<!--
		To compile DiSL bypass code, we need an extended Thread class
		containing a public "bypass" field used by the dynamic bypass
		code. When running an application with DiSL instrumentation,
		the Thread class will be extended at load time, but we need
		one also for compilation.

		Here we first check if the extended Thread class exists, and
378 379
		if not, we use a thread extended from build tools to generate
		the extended Thread class.
380
	-->
381
	<!-- pathconvert property="build.disl.thread.available" setonempty="false">
382
		<fileset dir="." includes="${build.disl.thread}/**/Thread.class"/>
383
	</pathconvert -->
384 385 386 387 388 389 390 391 392 393
	<target name="-check-disl-thread">
		<condition property="build.disl.thread.available"><and>
			<available file="${build.disl.thread}/java/lang/Thread.class"/>
			<length file="${build.disl.thread}/java/lang/Thread.class" when="greater" length="1024"/>
		</and></condition>
	</target>

	<target name="-create-disl-thread-if-needed" unless="build.disl.thread.available" depends="-check-disl-thread">
		<antcall target="-create-disl-thread"/>
	</target>
394

395
	<target name="-create-disl-thread" depends="compile-tools">
396 397
		<local name="extender.class"/>
		<find-class property="extender.class" dir="${build.tools}" name="ExtendThread"/>
398

399
		<mkdir dir="${build.disl.thread}"/>
400
		<java classname="${extender.class}" fork="true">
401
			<classpath>
402 403
				<path location="${build.tools}"/>
				<path refid="asm.classpath"/>
404
			</classpath>
405
			<arg value="${basedir}/${build.disl.thread}"/>
406
		</java>
Lukáš Marek's avatar
Lukáš Marek committed
407 408 409
	</target>


410 411 412 413 414 415
	<!--
		Compile multiple versions of the bypass code: one which always
		bypasses the instrumentation, one which never bypasses the
		instrumentation, and one that checks the "bypass" field in
		the extended Thread class to determine whether to bypass the
		instrumentation or not.
Lukáš Marek's avatar
Lukáš Marek committed
416

417 418 419 420 421 422 423
		After compiling, create a file set of default bypass classes.
		The DiSL agent requires those so that instrumented application
		can find the BypassCheck class, and after switch to dynamic
		bypass performed by the agent, also the DynamicBypass class.
		The DiSL server requires both classes to create class literals
		in the weaver code.
	-->
424
	<target name="compile-disl-bypass" depends="-init,-create-disl-thread-if-needed">
425
		<ac:for list="always,never,dynamic" param="variant">
426
			<sequential>
427
				<mkdir dir="${build.disl.bypass}/@{variant}"/>
428
				<javac destdir="${build.disl.bypass}/@{variant}" srcdir="${src.disl.bypass}/@{variant}" debug="true" source="${target.disl.bypass}" target="${target.disl.bypass}">
429
					<compilerarg value="-Xbootclasspath/p:${basedir}/${build.disl.thread}"/>
430 431
				</javac>
			</sequential>
432
		</ac:for>
433

434
		<union id="disl.bypass.files">
435 436
			<fileset dir="${build.disl.bypass}/always" includes="**/BypassCheck.class"/>
			<fileset dir="${build.disl.bypass}/dynamic" includes="**/DynamicBypass.class"/>
437
		</union>
438 439
	</target>

440

441 442 443 444 445 446 447 448
	<!--
		Package the default variant of the bypass code which will
		be included in the boot classpath. By default, we include
		a BypassCheck variant that always bypasses instrumentation.
		We also include DynamicBypass class that is needed by the
		dynamic variant of the BypassCheck class, which will be
		substituted by the DiSL agent in place of the default on.
	-->
449
	<target name="build-disl-bypass" depends="compile-disl-bypass">
450
		<jar destfile="${out.lib}/${disl.bypass.lib}">
451
			<resources refid="disl.bypass.files"/>
452
		</jar>
453 454
	</target>

455 456 457

	<!-- Shared utility code -->

458
	<target name="compile-util" depends="-init">
459
		<mkdir dir="${build.util}"/>
460
		<javac destdir="${build.util}" srcdir="${src.util}" debug="true">
461
			<classpath refid="asm.classpath"/>
462
			<classpath refid="log4j.classpath"/>
463
		</javac>
464 465 466

		<!-- Register logging providers -->
		<!--
467 468
		<local name="service.dir"/>
		<property name="service.dir" value="${build.util}/META-INF/services"/>
469

470 471
		<local name="logging.package"/>
		<property name="logging.package" value="ch.usi.dag.util.logging"/>
472

473 474 475 476
		<local name="service.file"/>
		<property name="service.file" value="${service.dir}/${logging.package}.Provider"/>

		<mkdir dir="${service.dir}"/>
477 478 479
		<echo file="${service.file}" append="false">${logging.package}.JavaProvider${line.separator}</echo>
		<echo file="${service.file}" append="true">${logging.package}.Log4jProvider${line.separator}</echo>
		-->
480 481
	</target>

482 483 484

	<!-- DiSL -->

485
	<target name="compile-disl" depends="compile-util,compile-disl-bypass">
486
		<ant antfile="${src.disl.prefix}/build.xml" target="compile" usenativebasedir="true"/>
487 488
	</target>

489 490 491
	<target name="compile-disl-server" depends="compile-disl">
		<ant dir="${src.disl-server.prefix}" target="compile"/>
	</target>
Lukáš Marek's avatar
Lukáš Marek committed
492

493

494 495 496
	<target name="build-disl" depends="compile-disl,build-disl-bypass">
		<ant dir="${src.disl-server.prefix}" target="build" />
		<copy file="${src.disl-server.prefix}/${out.lib}/${disl.lib}" tofile="${out.lib}/${disl.lib}" />
497 498
	</target>

499 500 501

	<!-- DiSL agent -->

502 503 504 505 506
	<!--
		The DiSL agent requires a header file containing flags for
		various code generation options. This target generates the
		header using "javah" after compiling DiSL.
	-->
507
	<target name="-check-disl-agent-codeflags">
508
		<property name="build.disl.agent.codeflags.path" value="${src.disl.agent}/codeflags.h"/>
509
		<uptodate property="build.disl.agent.codeflags.uptodate" targetfile="${build.disl.agent.codeflags.path}">
510
			<srcfiles dir="${src.disl.prefix}/src" includes="**/DiSL.java"/>
511
		</uptodate>
512 513
	</target>

514 515 516
	<target name="-generate-disl-agent-codeflags-if-needed" unless="build.disl.agent.codeflags.uptodate" depends="-check-disl-agent-codeflags">
		<antcall target="-generate-disl-agent-codeflags"/>
	</target>
517

518
	<target name="-generate-disl-agent-codeflags" depends="compile-disl">
519
		<local name="flags.class"/>
520
		<find-class property="flags.class" dir="${src.disl.prefix}/${build.disl}" name="*CodeOption$Flag"/>
521

522
		<javah outputFile="${build.disl.agent.codeflags.path}" force="yes">
523 524
			<class name="${flags.class}"/>
			<classpath refid="disl.classpath"/>
525 526 527 528 529 530
		</javah>
	</target>


	<!--
		The DiSL agent requires the bytecode of several bypass
531 532
		classes. This target generates the "bytecode.c" containing
		the necessary bytecode.
533
	-->
534
	<target name="-check-disl-agent-bytecode">
535
		<property name="build.disl.agent.bytecode.path" value="${src.disl.agent}/bytecode.c"/>
536
		<uptodate property="build.disl.agent.bytecode.uptodate" targetfile="${build.disl.agent.bytecode.path}">
537
			<srcfiles dir="${src.disl.bypass}"/>
538
		</uptodate>
539 540
	</target>

541
	<target name="-generate-disl-agent-bytecode-if-needed" unless="build.disl.agent.bytecode.uptodate" depends="-check-disl-agent-bytecode">
542
		<antcall target="-generate-disl-agent-bytecode"/>
543
	</target>
544

545
	<target name="-generate-disl-agent-bytecode" depends="compile-disl,compile-tools">
546 547
		<taskdef name="bytedump" classname="ch.usi.dag.disl.tools.ByteDumpTask">
			<classpath>
548
				<pathelement location="${build.tools}"/>
549 550 551
			</classpath>
		</taskdef>

552 553
		<local name="output.file"/>
		<property name="output.file" value="${build.disl.agent.bytecode.path}"/>
554

555 556 557
		<echo file="${output.file}">/**${line.separator}</echo>
		<echo file="${output.file}" append="true"> * DO NOT EDIT!${line.separator}</echo>
		<echo file="${output.file}" append="true"> *${line.separator}</echo>
558
		<echo file="${output.file}" append="true"> * This file was generated using the '-generate-disl-agent-bytecode' target.${line.separator}</echo>
559 560
		<echo file="${output.file}" append="true"> */${line.separator}</echo>
		<echo file="${output.file}" append="true">#include &lt;jvmti.h&gt;${line.separator}</echo>
561

562
		<ac:for param="file.path">
563
			<path>
564
				<fileset dir="${build.disl.bypass}" includes="**/BypassCheck.class"/>
565 566 567
				<!-- fileset dir="${build.disl.bypass}" includes="**/DynamicBypass.class" /-->
			</path>
			<sequential>
568
				<!-- Variant of the class, i.e., the top-level directory name. -->
569
				<local name="file.variant"/>
570
				<pathconvert property="file.variant">
571 572
					<path path="@{file.path}"/>
					<mapper type="regexp" from="${basedir}/${build.disl.bypass}/([^/]+)/.*" to="\1"/>
573
				</pathconvert>
574 575

				<!-- Name of the class. -->
576
				<local name="file.class"/>
577
				<pathconvert property="file.class">
578 579
					<path path="@{file.path}"/>
					<mapper type="regexp" from=".*/([^/]+).class" to="\1"/>
580 581
				</pathconvert>

582 583
				<local name="file.length"/>
				<length property="file.length" file="@{file.path}"/>
584 585

				<!-- Define a structure for each class file. -->
586 587 588
				<echo file="${output.file}" append="true">${line.separator}jvmtiClassDefinition ${file.variant}_${file.class}_classdef = {${line.separator}</echo>
				<echo file="${output.file}" append="true">&#9;.class_byte_count = ${file.length},${line.separator}</echo>
				<echo file="${output.file}" append="true">&#9;.class_bytes = (unsigned char *)${line.separator}</echo>
589
				<bytedump
590
					srcFile="@{file.path}" destFile="${output.file}" append="true"
591 592 593
					blockLength="16"  byteFormat="\x%02x"
					blockPrefix="&#9;&#9;&quot;" blockSuffix="&quot;${line.separator}" lastSuffix="&quot;,${line.separator}"
				/>
594
				<echo file="${output.file}" append="true">};${line.separator}</echo>
595
			</sequential>
596
		</ac:for>
597 598
	</target>

599

600
	<target name="prepare-disl-agent" depends="-generate-disl-agent-codeflags-if-needed,-generate-disl-agent-bytecode-if-needed"/>
601 602


603 604
	<target name="compile-disl-agent" depends="prepare-disl-agent">
		<exec executable="make" dir="${src.disl.agent}"/>
605 606 607 608 609 610 611 612
	</target>


	<!--
		This target cross-compiles the DiSL agent library for
		Windows using MinGW. It is currently not used in the
		default build, but should be used for binary releases.
	-->
613
	<target name="compile-disl-agent-mingw" depends="prepare-disl-agent">
614
		<exec executable="make" dir="${src.disl.agent}">
615 616
			<arg value="WHOLE=1"/>
			<arg value="TARGET_ENV=MinGW"/>
617
		</exec>
618 619 620
	</target>


621
	<target name="build-disl-agent" depends="compile-disl-agent,-determine-lib-names">
622
		<copy file="${src.disl.agent}/${disl.agent.os}-${disl.agent.arch}/${disl.agent.lib}" todir="${out.lib}"/>
623
	</target>
624 625


626
	<!-- Shadow VM -->
627

628
	<target name="compile-shvm-dispatch">
629 630
		<mkdir dir="${build.shvm.dispatch}"/>
		<javac destdir="${build.shvm.dispatch}" srcdir="${src.shvm.dispatch}" debug="true" source="${target.shvm.dispatch}" target="${target.shvm.dispatch}"/>
631 632 633
	</target>


634
	<target name="build-shvm-dispatch" depends="compile-shvm-dispatch">
635
		<jar destfile="${out.lib}/${shvm.dispatch.lib}" basedir="${build.shvm.dispatch}"/>
636 637 638
	</target>


639
	<target name="compile-shvm" depends="compile-util,compile-shvm-dispatch">
640
		<ant antfile="${src.shvm.prefix}/build.xml" target="compile" usenativebasedir="true"/>
641 642
	</target>

643

644
	<target name="build-shvm" depends="compile-shvm,build-shvm-dispatch">
645 646
		<ant antfile="${src.shvm-server.prefix}/build.xml" target="build" usenativebasedir="true"/>
		<copy file="${src.shvm-server.prefix}/${out.lib}/${shvm-server.lib}" tofile="${out.lib}/${shvm-server.lib}" />
647 648
	</target>

649
	<!-- Shadow VM agent -->
Lukáš Marek's avatar
Lukáš Marek committed
650

651
	<target name="compile-shvm-agent">
652
		<exec executable="make" dir="${src.shvm.agent}">
653
			<arg value="WHOLE=1"/>
654
		</exec>
655 656 657
	</target>


658
	<target name="build-shvm-agent" depends="compile-shvm-agent,-determine-lib-names">
659
		<copy file="${src.shvm.agent}/${shvm.agent.lib}" todir="${out.lib}"/>
660
	</target>
František Haas's avatar
František Haas committed
661

662

663
	<!-- JUnit tests and DiSL/Shadow VM test suites -->
664

665
	<path id="test.classpath">
666 667
		<path refid="util.classpath"/>
		<path refid="disl.classpath"/>
668
		<path refid="disl-server.classpath"/>
669
		<path refid="shvm.classpath"/>
670
		<path refid="shvm-server.classpath"/>
671 672
		<path refid="junit.classpath"/>
		<path refid="asm.classpath"/>
673 674
	</path>

675
	<target name="compile-test" depends="-init,compile-util,compile-disl,compile-shvm">
676
		<mkdir dir="${build.test}"/>
677
		<javac destdir="${build.test}" srcdir="${src.test}" debug="true">
678
			<classpath refid="test.classpath"/>
679
		</javac>
František Haas's avatar
František Haas committed
680

681 682 683
		<!-- Copy test resource files from source to class output directory. -->
		<copy todir="${build.test}">
			<fileset dir="${src.test}">
684
				<include name="**/*.resource"/>
685 686
			</fileset>
		</copy>
František Haas's avatar
František Haas committed
687 688 689
	</target>


690
	<macrodef name="package2path">
691 692
		<attribute name="package"/>
		<attribute name="property"/>
693 694
		<sequential>
			<pathconvert property="@{property}">
695 696
				<path path="@{package}"/>
				<mapper type="unpackage" from="${basedir}/*" to="*"/>
697 698 699
			</pathconvert>
		</sequential>
	</macrodef>
František Haas's avatar
František Haas committed
700

František Haas's avatar
František Haas committed
701

702
	<target name="build-test" depends="-init,compile-test">
703 704
		<property name="suite.base.pkg" value="ch.usi.dag.disl.test.suite"/>
		<package2path package="${suite.base.pkg}" property="suite.base.path"/>
705 706

		<condition property="test.suites" value="${test.name}" else="*">
707
			<isset property="test.name"/>
708
		</condition>
František Haas's avatar
František Haas committed
709

710
		<ac:for param="suite.path">
František Haas's avatar
František Haas committed
711
			<path>
712
				<dirset dir="${src.test}/${suite.base.path}">
713
					<include name="${test.suites}"/>
František Haas's avatar
František Haas committed
714 715 716
				</dirset>
			</path>
			<sequential>
717 718 719
				<local name="suite.name"/>
				<basename file="@{suite.path}" property="suite.name"/>
				<build-test-suite name="${suite.name}" path="${suite.base.path}/${suite.name}" pkg="${suite.base.pkg}.${suite.name}"/>
František Haas's avatar
František Haas committed
720
			</sequential>
721
		</ac:for>
František Haas's avatar
František Haas committed
722 723
	</target>

František Haas's avatar
František Haas committed
724

725 726 727 728 729 730 731
	<macrodef name="build-test-suite">
		<attribute name="name"/>
		<attribute name="path"/>
		<attribute name="pkg"/>
		<sequential>
			<!-- Package target application classes. -->
			<jar destfile="${out.test}/@{name}-app.jar">
732
				<fileset dir="${build.test}" includes="@{path}/app/**"/>
733
				<manifest>
734
					<attribute name="Main-Class" value="@{pkg}.app.TargetClass"/>
735 736 737 738 739 740 741 742 743 744 745 746
				</manifest>
			</jar>

			<!--
				Process annotations to find DiSL classes.

				Use an empty directory as destination for the classes to
				make the "javac" task actually execute "javac" with the
				source files. Using the normal build destination directory
				would result in "javac" task excluding all source files
				for which it could find up-to-date class files.
			-->
747 748
			<local name="classfinder.output"/>
			<tempfile property="classfinder.output" prefix="@{name}-disl-classes-" suffix=".lst" destDir="${out.dir}" createFile="true" deleteOnExit="true"/>
749

750
			<local name="classfinder.class"/>
751
			<find-class property="classfinder.class" dir="${src.disl.prefix}/${build.disl}" name="DislClassFinder"/>
752

753
			<mkdir dir="${out.dir}/empty"/>
754
			<javac srcdir="${src.test}" destDir="${out.dir}/empty">
755
				<classpath refid="test.classpath"/>
756

757 758 759 760
				<compilerArg value="-proc:only"/>
				<compilerArg line="-processor ${classfinder.class}"/>
				<compilerArg value="-Adisl.classfinder.output=${classfinder.output}"/>
				<compilerArg value="-Adisl.classfinder.separator=,"/>
761

762
				<include name="@{path}/**/*.java"/>
763 764
			</javac>

765 766
			<local name="disl.classes"/>
			<loadfile property="disl.classes" srcFile="${classfinder.output}"/>
767 768 769 770

			<!-- Package instrumentation classes. -->
			<jar destfile="${out.test}/@{name}-inst.jar">
				<manifest>
771
					<attribute name="DiSL-Classes" value="${disl.classes}"/>
772 773
				</manifest>

774
				<fileset dir="${build.test}" includes="@{path}/instr/**"/>
775 776 777
			</jar>
		</sequential>
	</macrodef>
František Haas's avatar
František Haas committed
778

779 780
	<!-- Run the instrumentation library unit tests -->
	<target name="test-disl">
781
		<ant antfile="${src.disl.prefix}/build.xml" target="test" usenativebasedir="true"/>
782 783 784 785
	</target>

	<!-- Run the shadow VM unit tests -->
	<target name="test-shvm">
786
		<ant antfile="${src.shvm.prefix}/build.xml" target="test" usenativebasedir="true" />
787 788 789
	</target>

	<!-- Run all tests -->
790
	<target name="test" depends="test-disl,test-shvm,test-compound"></target>
791 792

	<!-- Run compound tests -->
793
	<target name="test-compound" depends="build,build-test" description="Runs all tests or a selected (-Dtest.name=...) test suite.">
František Haas's avatar
František Haas committed
794

795 796 797 798
		<!--
			If test.name is set to a name of a test suite, only include the test suite
			in the batch of tests to be run, otherwise include all tests and suites.
		-->
799
		<ac:if>
800
			<isset property="test.name"/>
801
			<ac:then>
802 803
				<echo>Running a single test suite: ${test.name}</echo>
				<fileset id="test.batch" dir="${src.test}">
804
					<include name="**/suite/${test.name}/junit/*Test.java"/>
805
				</fileset>
806
			</ac:then><ac:else>
807 808
				<echo>Running all tests.</echo>
				<fileset id="test.batch" dir="${src.test}">
809 810 811
					<include name="**/junit/*Test.java"/>
					<include name="**/disl/*Test.java"/>
					<include name="**/disl/scope/*Test.java"/>
812
					<include name="**/disl/util/cfg/*Test.java"/>
813
					<include name="**/dislreserver/shadow/*Test.java"/>
814
				</fileset>
815 816
			</ac:else>
		</ac:if>
František Haas's avatar
František Haas committed
817

818 819 820
		<!--
			Run the batch of junit tests and junit-wrapped test suites.
		-->
821
		<mkdir dir="${out.junit}"/>
822
		<junit haltonfailure="yes" haltonerror="yes" failureproperty="junit.failure">
823 824
			<formatter type="brief" usefile="false"/>
			<formatter type="xml"/>
825 826

			<classpath>
827 828 829 830
				<path refid="asm.classpath"/>
				<path refid="junit.classpath"/>
				<pathelement location="${build.test}"/>
				<pathelement location="${build.util}"/>
831
				<pathelement location="${src.disl-server.prefix}/${build.disl-server}" />
832
				<pathelement location="${src.shvm-server.prefix}/${build.shvm-server}" />
833
				<pathelement location="${src.disl.prefix}/${build.disl}"/>
834
				<pathelement location="${src.shvm.prefix}/${build.shvm}" />
835 836
			</classpath>

837
			<!-- pass properties starting with "disl.", "dislserver.", and "dislreserver." to the tests -->
František Haas's avatar
František Haas committed
838
			<syspropertyset>
839 840 841
				<propertyref prefix="disl."/>
				<propertyref prefix="dislserver."/>
				<propertyref prefix="dislreserver."/>
František Haas's avatar
František Haas committed
842
			</syspropertyset>
843

844 845 846 847 848 849
			<sysproperty key="runner.disl.lib.dir" value="${out.lib}"/>
			<sysproperty key="runner.lib.dir" value="${out.test}"/>
			<sysproperty key="runner.disl.agent.lib" value="${disl.agent.lib}"/>
			<sysproperty key="runner.shvm.agent.lib" value="${shvm.agent.lib}"/>
			<sysproperty key="runner.debug" value="${test.debug}"/>
			<sysproperty key="test.verbose" value="${test.verbose}"/>
850

851
			<batchtest fork="yes" todir="${out.junit}">
852
				<fileset refid="test.batch"/>
František Haas's avatar
František Haas committed
853 854 855
			</batchtest>
		</junit>
	</target>
856 857 858 859


	<!-- Eclipse support -->

860
	<target name="build-eclipse-agent">
861 862
		<local name="agent.class"/>
		<find-class property="agent.class" dir="${src.test}" name="Agent" suffix=".java"/>
863 864

		<jar destfile="${out.lib}/${eclipse.agent.lib}">
865
			<manifest>
866
				<attribute name="Premain-Class" value="${agent.class}"/>
867 868 869 870 871 872 873
			</manifest>
		</jar>
	</target>


	<!-- Documentation -->

874
	<macrodef name="pdflatex">
875 876
		<attribute name="dir" default="."/>
		<element name="exec-elements" implicit="yes"/>
877
		<sequential>
878 879
			<local name="pdflatex.output"/>
			<local name="pdflatex.result"/>
880

881
			<exec executable="pdflatex" dir="@{dir}" outputProperty="pdflatex.output" resultProperty="pdflatex.result">
882
				<exec-elements/>
883 884
			</exec>
			<fail message="${pdflatex.output}">
885
				<condition><isfailure code="${pdflatex.result}"/></condition>
886 887 888 889 890 891
			</fail>
		</sequential>
	</macrodef>


	<target name="check-doc">
892 893 894
		<property name="doc.intro.tex" value="dislintro.tex"/>
		<property name="doc.intro.pdf" value="${build.doc.intro}/dislintro.pdf"/>
		<fileset id="doc.intro.files" dir="${src.doc.intro}"/>
895

896
		<uptodate property="doc.intro.uptodate" targetFile="${doc.intro.pdf}">
897
			<srcfiles refid="doc.intro.files"/>
898 899 900 901 902
		</uptodate>
	</target>


	<target name="build-doc" depends="check-doc" unless="doc.intro.uptodate">
903
		<delete dir="${build.doc.intro}"/>
904
		<copy todir="${build.doc.intro}">
905
			<fileset refid="doc.intro.files"/>
906
		</copy>
907 908 909

		<echo>Compiling Introduction to Instrumentation with DiSL</echo>
		<pdflatex dir="${build.doc.intro}">
910 911
			<arg value="-draftmode"/>
			<arg value="${doc.intro.tex}"/>
912 913 914
		</pdflatex>

		<pdflatex dir="${build.doc.intro}">
915
			<arg value="${doc.intro.tex}"/>
916
		</pdflatex>
917 918 919
	</target>


920
	<target name="build-jdoc" depends="-init,compile">
921
		<mkdir dir="${out.doc.jdoc}"/>
922 923
		<javadoc destdir="${out.doc.jdoc}"
			access="public" author="true" version="true" use="true"
924 925
			noDeprecated="false" nodeprecatedlist="false"
			noIndex="false" splitIndex="true" noNavbar="false" noTree="false"
926
			overview="${src.doc.jdoc}/overview.html"
927
			source="1.8"
928 929
		>
			<sourcepath>
930
				<pathelement path="${src.disl.prefix}/src"/>
931 932 933
			</sourcepath>

			<sourcefiles>
934
				<fileset dir="${src.disl.prefix}/src">
935 936 937 938 939 940
					<include name="**/DiSL.java"/>
					<include name="**/Reflection.java"/>
					<include name="**/Scope.java"/>
					<include name="**/ScopeMatcher.java"/>
					<include name="**/Shadow.java"/>
					<include name="**/Transformer.java"/>
941 942
				</fileset>
				<fileset dir="${src.disl.bypass}">
943
					<include name="**/DynamicBypass.java"/>
944
				</fileset>
945 946

				<fileset dir="${src.shvm}">
947 948 949 950 951
					<include name="**/RemoteAnalysis.java"/>
					<include name="**/ShadowObject.java"/>
					<include name="**/ShadowString.java"/>
					<include name="**/ShadowThread.java"/>
					<include name="**/ShadowClass.java"/>
952 953
				</fileset>
				<fileset dir="${src.shvm.dispatch}">
954
					<include name="**/REDispatch.java"/>
955
				</fileset>
956 957 958
			</sourcefiles>

			<classpath>
959 960 961 962
				<path refid="disl.bypass.classpath"/>
				<path refid="shvm.classpath"/>
				<path refid="util.classpath"/>
				<path refid="asm.classpath"/>
963 964 965 966 967 968 969
			</classpath>
		</javadoc>
	</target>


	<!-- Miscellaneous -->

970
	<target name="copy-libs" depends="-init">
971 972
		<copy todir="${out.lib}" flatten="true">
			<path refid="asm.classpath"/>
973
			<path refid="protobuf.classpath"/>
974
		</copy>
975 976 977 978
	</target>


	<target name="package-src">
979
		<zip destfile="${out.src}/src.zip">
980
			<zipfileset dir="${src.disl.prefix}/src"/>
981 982 983 984
			<zipfileset dir="${src.disl.bypass}/dynamic"/>
			<zipfileset dir="${src.shvm}"/>
			<zipfileset dir="${src.shvm.dispatch}"/>
			<zipfileset dir="${src.util}"/>
985 986 987 988 989 990
		</zip>
	</target>


	<!-- Releases -->

991
	<target name="release-bin" depends="build,package-src">
992
		<!-- Collect the artifacts from output and source directories. -->
993
		<zip destfile="${release.bin.zip}">
994
			<zipfileset prefix="${disl.prefix}/${dist.bin}" dir="${src.bin}" fileMode="755"/>
995
			<zipfileset prefix="${disl.prefix}/${dist.lib}" dir="${out.lib}"/>
996 997
			<zipfileset prefix="${disl.prefix}/${dist.doc.intro}" file="${doc.intro.pdf}"/>
			<zipfileset prefix="${disl.prefix}/${dist.doc.jdoc}" dir="${out.doc.jdoc}"/>
998
			<zipfileset prefix="${disl.prefix}/${dist.examples}" dir="${src.examples}">
999
				<exclude name="build-override.properties"/>
1000
			</zipfileset>
1001
			<zipfileset prefix="${disl.prefix}/${dist.src}" dir="${out.src}"/>
1002
			<zipfileset prefix="${disl.prefix}" dir=".">
1003 1004 1005
				<include name="COPYING"/>
				<include name="README"/>
				<include name="USER_ERRORS"/>
1006
			</zipfileset>
1007 1008
		</zip>

1009
		<!-- Convert the zip archive to bzip2 compressed tar. -->
1010
		<tar destfile="${release.bin.tar}" compression="bzip2">
1011
			<zipfileset src="${release.bin.zip}"/>
1012 1013 1014 1015
		</tar>
	</target>


1016
	<target name="release-src" depends="clean-examples">
1017 1018
		<zip destfile="${release.src.zip}">
			<zipfileset prefix="disl-${disl.version}" dir=".">
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
				<include name="${src.bin}/"/>
				<include name="${src.doc}/"/>
				<include name="${src.examples}/"/>
				<include name="${lib.dir}/README"/>
				<include name="${lib.dir}/*.lst"/>
				<include name="COPYING"/>
				<include name="README"/>
				<include name="README_TESTS"/>
				<include name="USER_ERRORS"/>
				<include name="build.properties"/>
				<include name="build.xml"/>
				<include name="dependencies.properties"/>
				<include name="${build.ivy.file}"/>
				<include name="disl.version"/>
				<include name=".settings/"/>
				<include name=".project"/>
				<include name=".classpath"/>
				<include name="eclipse.*"/>
1037
			</zipfileset>
1038 1039
			<zipfileset prefix="disl-${disl.version}" dir="." includes="src*/" excludes="**/var,**/find.sh,**/*.so,**.jnilib,**/*.o"/>
			<zipfileset prefix="disl-${disl.version}" dir="." includes="src*/**/find.sh" filemode="755"/>
1040 1041 1042
		</zip>

		<tar destfile="${release.src.tar}" compression="bzip2">
1043
			<zipfileset src="${release.src.zip}"/>
1044 1045 1046 1047 1048
		</tar>
	</target>


	<!-- Summary targets -->
1049 1050 1051
        <target name="properties" depends="-determine-lib-names">
		<echoproperties/>
        </target>
1052

1053
	<target name="compile" depends="compile-disl,compile-disl-agent,compile-shvm,compile-shvm-agent" description="Compiles DiSL, Shadow VM, and the JVM agents for both."/>
1054

1055
	<target name="build" depends="build-disl,build-disl-agent,build-shvm,build-shvm-agent,copy-libs" description="Builds DiSL and Shadow VM, without documentation."/>
1056

1057
	<target name="eclipse" depends="download-sources,build-eclipse-agent,build-disl-bypass" description="Creates libraries for developing DiSL under Eclipse."/>
1058

1059
	<target name="doc" depends="build-doc,build-jdoc" description="Builds DiSL documentation (requires javadoc and pdflatex)."/>
1060

1061
	<target name="release" depends="doc,release-bin,release-src" description="Produces DiSL source and binary release archives."/>
1062 1063 1064 1065


	<!-- Clean up -->

1066
	<target name="clean-examples">
1067
		<ant dir="${src.examples}" target="clean"/>
1068 1069
	</target>

1070

1071
	<target name="clean" description="Removes all files produced during the build.">
1072

1073 1074 1075
		<ant dir="${src.shvm.prefix}" target="clean" />
		<ant dir="${src.disl-server.prefix}" target="clean" />
		<ant dir="${src.disl.prefix}" target="clean" />
1076

1077
		<exec executable="make" dir="${src.disl.agent}">
1078
			<arg value="cleanall"/>
1079 1080 1081
		</exec>

		<exec executable="make" dir="${src.shvm.agent}">
1082
			<arg value="cleanall"/>
1083
		</exec>
1084

1085
		<delete dir="${out.dir}"/>
1086 1087
	</target>

1088 1089 1090 1091 1092

	<target name="clean-deps" description="Removes all downloaded dependency artifacts.">
		<delete dir="${lib.ant.dir}"/>
		<delete dir="${lib.disl.dir}"/>
		<delete dir="${lib.test.dir}"/>
1093
		<delete dir="${lib.devel.dir}"/>
1094 1095
	</target>

1096
</project>