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"/>
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 19 20 21
	<!-- ================================================================== -->
	<!-- IVY SETUP & TASKS                                                  -->
	<!-- ================================================================== -->

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


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

39

40 41 42 43 44 45
	<!--
		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}"/>
46
		<ivy:retrieve pattern="${lib.dir}/[conf]/[artifact].[ext]"/>
47
	</target>
František Haas's avatar
František Haas committed
48

49

50 51 52 53 54 55 56
	<!--
		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>
57 58


59 60 61
	<!-- ================================================================== -->
	<!-- ARTIFACT LIST                                                      -->
	<!-- ================================================================== -->
62

63
	<!--
64
		Downloads dependencies and stores the list of retrieved artifacts
65
		so that their presence can be checked in subsequent invocations
66
		without Ivy. This target should allow unconditional execution so
67 68 69 70
		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"/>
71

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

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		<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}">
101
				<srcfiles dir="${basedir}" includes="dependencies*.properties"/>
102 103 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
			</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.
156
		This may trigger creating the list of expected artifacts after an
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		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">
172
		<antcall target="download-deps"/>
173 174 175 176 177 178 179 180 181 182 183 184 185 186
	</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"/>
187
		<path-to-text
188 189 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
			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">
229
			<fileset dir="${lib.ant.dir}" includes="*.jar"/>
230 231
		</path>

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

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

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

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

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

252 253 254 255 256 257 258 259 260
		<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>

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

		<path id="disl.classpath">
267
			<pathelement location="${build.disl}"/>
268 269 270
		</path>

		<path id="disl.bypass.classpath">
271
			<pathelement location="${build.disl.bypass}/dynamic"/>
272 273 274
		</path>

		<path id="shvm.classpath">
275 276
			<pathelement location="${build.shvm}"/>
			<pathelement location="${build.shvm.dispatch}"/>
277 278 279 280 281 282 283 284 285 286
		</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>
287
			<os family="windows"/>
288
			<ac:then>
289 290
				<property name="dl.prefix" value=""/>
				<property name="dl.suffix" value=".dll"/>
291
				<property name="disl.agent.os" value="windows"/>
292 293
			</ac:then><ac:else>
				<ac:if>
294
					<os family="unix"/>
295
					<ac:then>
296
						<property name="dl.prefix" value="lib"/>
297
						<ac:if>
298
							<os name="Mac OS X"/>
299
							<ac:then>
300
								<property name="dl.suffix" value=".jnilib"/>
301
								<property name="disl.agent.os" value="macosx"/>
302
							</ac:then><ac:else>
303
								<property name="dl.suffix" value=".so"/>
304
								<property name="disl.agent.os" value="linux"/>
305 306 307
							</ac:else>
						</ac:if>
					</ac:then><ac:else>
308
						<fail message="Unsupported operating system: ${os.name}"/>
309 310 311 312
					</ac:else>
				</ac:if>
			</ac:else>
		</ac:if>
313

314 315 316 317 318 319
		<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>

320 321
		<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}"/>
322 323 324
	</target>


325 326 327 328 329 330
	<!--
		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">
331 332 333 334 335
		<attribute name="dir"/>
		<attribute name="name"/>
		<attribute name="property"/>
		<attribute name="suffix" default=".class"/>
		<attribute name="setonempty" default="true"/>
336 337
		<sequential>
			<pathconvert property="@{property}" setonempty="@{setonempty}">
338 339
				<fileset dir="@{dir}" includes="**/@{name}@{suffix}"/>
				<mapper type="package" from="${basedir}/@{dir}/*@{suffix}" to="*"/>
340 341 342 343 344
			</pathconvert>
		</sequential>
	</macrodef>


345 346
	<!-- Build tools -->

347
	<target name="compile-tools" depends="-init">
348
		<mkdir dir="${build.tools}"/>
349 350
		<javac destdir="${build.tools}" srcdir="${src.tools}">
			<classpath>
351 352
				<path refid="asm.classpath"/>
				<path location="${ant.core.lib}"/>
353 354 355 356 357
			</classpath>
		</javac>
	</target>


358 359
	<!-- DiSL bypass -->

360 361 362 363 364 365 366 367
	<!--
		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
368 369
		if not, we use a thread extended from build tools to generate
		the extended Thread class.
370
	-->
371
	<!-- pathconvert property="build.disl.thread.available" setonempty="false">
372
		<fileset dir="." includes="${build.disl.thread}/**/Thread.class"/>
373
	</pathconvert -->
374 375 376 377 378 379 380 381 382 383
	<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>
384

385
	<target name="-create-disl-thread" depends="compile-tools">
386 387
		<local name="extender.class"/>
		<find-class property="extender.class" dir="${build.tools}" name="ExtendThread"/>
388

389
		<mkdir dir="${build.disl.thread}"/>
390
		<java classname="${extender.class}" fork="true">
391
			<classpath>
392 393
				<path location="${build.tools}"/>
				<path refid="asm.classpath"/>
394
			</classpath>
395
			<arg value="${basedir}/${build.disl.thread}"/>
396
		</java>
Lukáš Marek's avatar
Lukáš Marek committed
397 398 399
	</target>


400 401 402 403 404 405
	<!--
		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
406

407 408 409 410 411 412 413
		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.
	-->
414
	<target name="compile-disl-bypass" depends="-init,-create-disl-thread-if-needed">
415
		<ac:for list="always,never,dynamic" param="variant">
416
			<sequential>
417
				<mkdir dir="${build.disl.bypass}/@{variant}"/>
418
				<javac destdir="${build.disl.bypass}/@{variant}" srcdir="${src.disl.bypass}/@{variant}" debug="true" source="${target.disl.bypass}" target="${target.disl.bypass}">
419
					<compilerarg value="-Xbootclasspath/p:${basedir}/${build.disl.thread}"/>
420 421
				</javac>
			</sequential>
422
		</ac:for>
423

424
		<union id="disl.bypass.files">
425 426
			<fileset dir="${build.disl.bypass}/always" includes="**/BypassCheck.class"/>
			<fileset dir="${build.disl.bypass}/dynamic" includes="**/DynamicBypass.class"/>
427
		</union>
428 429
	</target>

430

431 432 433 434 435 436 437 438
	<!--
		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.
	-->
439
	<target name="build-disl-bypass" depends="compile-disl-bypass">
440
		<jar destfile="${out.lib}/${disl.bypass.lib}">
441
			<resources refid="disl.bypass.files"/>
442
		</jar>
443 444
	</target>

445 446 447

	<!-- Shared utility code -->

448
	<target name="compile-util" depends="-init">
449
		<mkdir dir="${build.util}"/>
450
		<javac destdir="${build.util}" srcdir="${src.util}" debug="true">
451
			<classpath refid="asm.classpath"/>
452
			<classpath refid="log4j.classpath"/>
453
		</javac>
454 455 456

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

460 461
		<local name="logging.package"/>
		<property name="logging.package" value="ch.usi.dag.util.logging"/>
462

463 464 465 466
		<local name="service.file"/>
		<property name="service.file" value="${service.dir}/${logging.package}.Provider"/>

		<mkdir dir="${service.dir}"/>
467 468 469
		<echo file="${service.file}" append="false">${logging.package}.JavaProvider${line.separator}</echo>
		<echo file="${service.file}" append="true">${logging.package}.Log4jProvider${line.separator}</echo>
		-->
470 471
	</target>

472 473 474

	<!-- DiSL -->

475
	<target name="compile-disl" depends="compile-util,compile-disl-bypass">
476
		<mkdir dir="${build.disl}"/>
477
		<javac destdir="${build.disl}" srcdir="${src.disl}" debug="true">
478
			<classpath>
479 480 481
				<path refid="disl.bypass.classpath"/>
				<path refid="util.classpath"/>
				<path refid="asm.classpath"/>
482
				<path refid="protobuf.classpath"/>
483
			</classpath>
484 485 486
		</javac>
	</target>

Lukáš Marek's avatar
Lukáš Marek committed
487

488
	<target name="build-disl" depends="compile-disl,build-disl-bypass">
489 490 491 492 493
		<local name="server.class"/>
		<find-class property="server.class" dir="${build.disl}" name="DiSLServer"/>

		<local name="classfinder.class"/>
		<find-class property="classfinder.class" dir="${build.disl}" name="DislClassFinder"/>
494 495

		<jar destfile="${out.lib}/${disl.lib}">
496
			<manifest>
497
				<attribute name="Class-Path" value="${asm.libs} ${protobuf.libs}"/>
498 499
				<attribute name="Main-Class" value="${server.class}"/>
				<attribute name="DiSL-Version" value="${disl.version}"/>
500
			</manifest>
501

502
			<service type="javax.annotation.processing.Processor" provider="${classfinder.class}"/>
503

504 505 506
			<fileset dir="${build.util}"/>
			<fileset dir="${build.disl}"/>
			<resources refid="disl.bypass.files"/>
507 508 509
		</jar>
	</target>

510 511 512

	<!-- DiSL agent -->

513 514 515 516 517
	<!--
		The DiSL agent requires a header file containing flags for
		various code generation options. This target generates the
		header using "javah" after compiling DiSL.
	-->
518
	<target name="-check-disl-agent-codeflags">
519
		<property name="build.disl.agent.codeflags.path" value="${src.disl.agent}/codeflags.h"/>
520
		<uptodate property="build.disl.agent.codeflags.uptodate" targetfile="${build.disl.agent.codeflags.path}">
521
			<srcfiles dir="${src.disl}" includes="**/DiSL.java"/>
522
		</uptodate>
523 524
	</target>

525 526 527
	<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>
528

529
	<target name="-generate-disl-agent-codeflags" depends="compile-disl">
530 531
		<local name="flags.class"/>
		<find-class property="flags.class" dir="${build.disl}" name="*CodeOption$Flag"/>
532

533
		<javah outputFile="${build.disl.agent.codeflags.path}" force="yes">
534 535
			<class name="${flags.class}"/>
			<classpath refid="disl.classpath"/>
536 537 538 539 540 541
		</javah>
	</target>


	<!--
		The DiSL agent requires the bytecode of several bypass
542 543
		classes. This target generates the "bytecode.c" containing
		the necessary bytecode.
544
	-->
545
	<target name="-check-disl-agent-bytecode">
546
		<property name="build.disl.agent.bytecode.path" value="${src.disl.agent}/bytecode.c"/>
547
		<uptodate property="build.disl.agent.bytecode.uptodate" targetfile="${build.disl.agent.bytecode.path}">
548
			<srcfiles dir="${src.disl.bypass}"/>
549
		</uptodate>
550 551
	</target>

552
	<target name="-generate-disl-agent-bytecode-if-needed" unless="build.disl.agent.bytecode.uptodate" depends="-check-disl-agent-bytecode">
553
		<antcall target="-generate-disl-agent-bytecode"/>
554
	</target>
555

556
	<target name="-generate-disl-agent-bytecode" depends="compile-disl,compile-tools">
557 558
		<taskdef name="bytedump" classname="ch.usi.dag.disl.tools.ByteDumpTask">
			<classpath>
559
				<pathelement location="${build.tools}"/>
560 561 562
			</classpath>
		</taskdef>

563 564
		<local name="output.file"/>
		<property name="output.file" value="${build.disl.agent.bytecode.path}"/>
565

566 567 568
		<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>
569
		<echo file="${output.file}" append="true"> * This file was generated using the '-generate-disl-agent-bytecode' target.${line.separator}</echo>
570 571
		<echo file="${output.file}" append="true"> */${line.separator}</echo>
		<echo file="${output.file}" append="true">#include &lt;jvmti.h&gt;${line.separator}</echo>
572

573
		<ac:for param="file.path">
574
			<path>
575
				<fileset dir="${build.disl.bypass}" includes="**/BypassCheck.class"/>
576 577 578
				<!-- fileset dir="${build.disl.bypass}" includes="**/DynamicBypass.class" /-->
			</path>
			<sequential>
579
				<!-- Variant of the class, i.e., the top-level directory name. -->
580
				<local name="file.variant"/>
581
				<pathconvert property="file.variant">
582 583
					<path path="@{file.path}"/>
					<mapper type="regexp" from="${basedir}/${build.disl.bypass}/([^/]+)/.*" to="\1"/>
584
				</pathconvert>
585 586

				<!-- Name of the class. -->
587
				<local name="file.class"/>
588
				<pathconvert property="file.class">
589 590
					<path path="@{file.path}"/>
					<mapper type="regexp" from=".*/([^/]+).class" to="\1"/>
591 592
				</pathconvert>

593 594
				<local name="file.length"/>
				<length property="file.length" file="@{file.path}"/>
595 596

				<!-- Define a structure for each class file. -->
597 598 599
				<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>
600
				<bytedump
601
					srcFile="@{file.path}" destFile="${output.file}" append="true"
602 603 604
					blockLength="16"  byteFormat="\x%02x"
					blockPrefix="&#9;&#9;&quot;" blockSuffix="&quot;${line.separator}" lastSuffix="&quot;,${line.separator}"
				/>
605
				<echo file="${output.file}" append="true">};${line.separator}</echo>
606
			</sequential>
607
		</ac:for>
608 609
	</target>

610

611
	<target name="prepare-disl-agent" depends="-generate-disl-agent-codeflags-if-needed,-generate-disl-agent-bytecode-if-needed"/>
612 613


614 615
	<target name="compile-disl-agent" depends="prepare-disl-agent">
		<exec executable="make" dir="${src.disl.agent}"/>
616 617 618 619 620 621 622 623
	</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.
	-->
624
	<target name="compile-disl-agent-mingw" depends="prepare-disl-agent">
625
		<exec executable="make" dir="${src.disl.agent}">
626 627
			<arg value="WHOLE=1"/>
			<arg value="TARGET_ENV=MinGW"/>
628
		</exec>
629 630 631
	</target>


632
	<target name="build-disl-agent" depends="compile-disl-agent,-determine-lib-names">
633
		<copy file="${src.disl.agent}/${disl.agent.os}-${disl.agent.arch}/${disl.agent.lib}" todir="${out.lib}"/>
634
	</target>
635 636


637
	<!-- Shadow VM -->
638

639
	<target name="compile-shvm-dispatch">
640 641
		<mkdir dir="${build.shvm.dispatch}"/>
		<javac destdir="${build.shvm.dispatch}" srcdir="${src.shvm.dispatch}" debug="true" source="${target.shvm.dispatch}" target="${target.shvm.dispatch}"/>
642 643 644
	</target>


645
	<target name="build-shvm-dispatch" depends="compile-shvm-dispatch">
646
		<jar destfile="${out.lib}/${shvm.dispatch.lib}" basedir="${build.shvm.dispatch}"/>
647 648 649
	</target>


650
	<target name="compile-shvm" depends="compile-util,compile-shvm-dispatch">
651
		<mkdir dir="${build.shvm}"/>
652
		<javac destdir="${build.shvm}" srcdir="${src.shvm}" debug="true">
653 654
			<classpath refid="util.classpath"/>
			<classpath refid="asm.classpath"/>
655
		</javac>
656 657
	</target>

658

659
	<target name="build-shvm" depends="compile-shvm,build-shvm-dispatch">
660 661
		<local name="shvm.server.class"/>
		<find-class property="shvm.server.class" dir="${build.shvm}" name="DiSLREServer"/>
662

663
		<jar destfile="${out.lib}/${shvm.lib}">
664
			<manifest>
665 666 667
				<attribute name="Class-Path" value="${asm.libs}"/>
				<attribute name="Main-Class" value="${shvm.server.class}"/>
				<attribute name="DiSL-Version" value="${disl.version}"/>
668
			</manifest>
669

670 671
			<fileset dir="${build.util}"/>
			<fileset dir="${build.shvm}"/>
672 673 674
		</jar>
	</target>

675
	<!-- Shadow VM agent -->
Lukáš Marek's avatar
Lukáš Marek committed
676

677
	<target name="compile-shvm-agent">
678
		<exec executable="make" dir="${src.shvm.agent}">
679
			<arg value="WHOLE=1"/>
680
		</exec>
681 682 683
	</target>


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

688

689
	<!-- JUnit tests and DiSL/Shadow VM test suites -->
690

691
	<path id="test.classpath">
692 693 694 695 696
		<path refid="util.classpath"/>
		<path refid="disl.classpath"/>
		<path refid="shvm.classpath"/>
		<path refid="junit.classpath"/>
		<path refid="asm.classpath"/>
697 698
	</path>

699
	<target name="compile-test" depends="-init,compile-util,compile-disl,compile-shvm">
700
		<mkdir dir="${build.test}"/>
701
		<javac destdir="${build.test}" srcdir="${src.test}" debug="true">
702
			<classpath refid="test.classpath"/>
703
		</javac>
František Haas's avatar
František Haas committed
704

705 706 707
		<!-- Copy test resource files from source to class output directory. -->
		<copy todir="${build.test}">
			<fileset dir="${src.test}">
708
				<include name="**/*.resource"/>
709 710
			</fileset>
		</copy>
František Haas's avatar
František Haas committed
711 712 713
	</target>


714
	<macrodef name="package2path">
715 716
		<attribute name="package"/>
		<attribute name="property"/>
717 718
		<sequential>
			<pathconvert property="@{property}">
719 720
				<path path="@{package}"/>
				<mapper type="unpackage" from="${basedir}/*" to="*"/>
721 722 723
			</pathconvert>
		</sequential>
	</macrodef>
František Haas's avatar
František Haas committed
724

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

726
	<target name="build-test" depends="-init,compile-test">
727 728
		<property name="suite.base.pkg" value="ch.usi.dag.disl.test.suite"/>
		<package2path package="${suite.base.pkg}" property="suite.base.path"/>
729 730

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

734
		<ac:for param="suite.path">
František Haas's avatar
František Haas committed
735
			<path>
736
				<dirset dir="${src.test}/${suite.base.path}">
737
					<include name="${test.suites}"/>
František Haas's avatar
František Haas committed
738 739 740
				</dirset>
			</path>
			<sequential>
741 742 743
				<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
744
			</sequential>
745
		</ac:for>
František Haas's avatar
František Haas committed
746 747
	</target>

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

749 750 751 752 753 754 755
	<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">
756
				<fileset dir="${build.test}" includes="@{path}/app/**"/>
757
				<manifest>
758
					<attribute name="Main-Class" value="@{pkg}.app.TargetClass"/>
759 760 761 762 763 764 765 766 767 768 769 770
				</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.
			-->
771 772
			<local name="classfinder.output"/>
			<tempfile property="classfinder.output" prefix="@{name}-disl-classes-" suffix=".lst" destDir="${out.dir}" createFile="true" deleteOnExit="true"/>
773

774 775
			<local name="classfinder.class"/>
			<find-class property="classfinder.class" dir="${build.disl}" name="DislClassFinder"/>
776

777
			<mkdir dir="${out.dir}/empty"/>
778
			<javac srcdir="${src.test}" destDir="${out.dir}/empty">
779
				<classpath refid="test.classpath"/>
780

781 782 783 784
				<compilerArg value="-proc:only"/>
				<compilerArg line="-processor ${classfinder.class}"/>
				<compilerArg value="-Adisl.classfinder.output=${classfinder.output}"/>
				<compilerArg value="-Adisl.classfinder.separator=,"/>
785

786
				<include name="@{path}/**/*.java"/>
787 788
			</javac>

789 790
			<local name="disl.classes"/>
			<loadfile property="disl.classes" srcFile="${classfinder.output}"/>
791 792 793 794

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

798
				<fileset dir="${build.test}" includes="@{path}/instr/**"/>
799 800 801
			</jar>
		</sequential>
	</macrodef>
František Haas's avatar
František Haas committed
802 803


804
	<target name="test" depends="build,build-test" description="Runs all tests or a selected (-Dtest.name=...) test suite.">
805 806 807 808
		<!--
			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.
		-->
809
		<ac:if>
810
			<isset property="test.name"/>
811
			<ac:then>
812 813
				<echo>Running a single test suite: ${test.name}</echo>
				<fileset id="test.batch" dir="${src.test}">
814
					<include name="**/suite/${test.name}/junit/*Test.java"/>
815
				</fileset>
816
			</ac:then><ac:else>
817 818
				<echo>Running all tests.</echo>
				<fileset id="test.batch" dir="${src.test}">
819 820 821
					<include name="**/junit/*Test.java"/>
					<include name="**/disl/*Test.java"/>
					<include name="**/disl/scope/*Test.java"/>
822
					<include name="**/disl/util/cfg/*Test.java"/>
823
					<include name="**/dislreserver/shadow/*Test.java"/>
824
				</fileset>
825 826
			</ac:else>
		</ac:if>
František Haas's avatar
František Haas committed
827

828 829 830
		<!--
			Run the batch of junit tests and junit-wrapped test suites.
		-->
831
		<mkdir dir="${out.junit}"/>
František Haas's avatar
František Haas committed
832
		<junit haltonfailure="no" haltonerror="no" failureproperty="junit.failure">
833 834
			<formatter type="brief" usefile="false"/>
			<formatter type="xml"/>
835 836

			<classpath>
837 838 839 840 841 842
				<path refid="asm.classpath"/>
				<path refid="junit.classpath"/>
				<pathelement location="${build.test}"/>
				<pathelement location="${build.util}"/>
				<pathelement location="${build.disl}"/>
				<pathelement location="${build.shvm}"/>
843 844
			</classpath>

845
			<!-- pass properties starting with "disl.", "dislserver.", and "dislreserver." to the tests -->
František Haas's avatar
František Haas committed
846
			<syspropertyset>
847 848 849
				<propertyref prefix="disl."/>
				<propertyref prefix="dislserver."/>
				<propertyref prefix="dislreserver."/>
František Haas's avatar
František Haas committed
850
			</syspropertyset>
851

852 853 854 855 856 857
			<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}"/>
858

859
			<batchtest fork="yes" todir="${out.junit}">
860
				<fileset refid="test.batch"/>
František Haas's avatar
František Haas committed
861 862 863
			</batchtest>
		</junit>
	</target>
864 865 866 867


	<!-- Eclipse support -->

868
	<target name="build-eclipse-agent">
869 870
		<local name="agent.class"/>
		<find-class property="agent.class" dir="${src.test}" name="Agent" suffix=".java"/>
871 872

		<jar destfile="${out.lib}/${eclipse.agent.lib}">
873
			<manifest>
874
				<attribute name="Premain-Class" value="${agent.class}"/>
875 876 877 878 879 880 881
			</manifest>
		</jar>
	</target>


	<!-- Documentation -->

882
	<macrodef name="pdflatex">
883 884
		<attribute name="dir" default="."/>
		<element name="exec-elements" implicit="yes"/>
885
		<sequential>
886 887
			<local name="pdflatex.output"/>
			<local name="pdflatex.result"/>
888

889
			<exec executable="pdflatex" dir="@{dir}" outputProperty="pdflatex.output" resultProperty="pdflatex.result">
890
				<exec-elements/>
891 892
			</exec>
			<fail message="${pdflatex.output}">
893
				<condition><isfailure code="${pdflatex.result}"/></condition>
894 895 896 897 898 899
			</fail>
		</sequential>
	</macrodef>


	<target name="check-doc">
900 901 902
		<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}"/>
903

904
		<uptodate property="doc.intro.uptodate" targetFile="${doc.intro.pdf}">
905
			<srcfiles refid="doc.intro.files"/>
906 907 908 909 910
		</uptodate>
	</target>


	<target name="build-doc" depends="check-doc" unless="doc.intro.uptodate">
911
		<delete dir="${build.doc.intro}"/>
912
		<copy todir="${build.doc.intro}">
913
			<fileset refid="doc.intro.files"/>
914
		</copy>
915 916 917

		<echo>Compiling Introduction to Instrumentation with DiSL</echo>
		<pdflatex dir="${build.doc.intro}">
918 919
			<arg value="-draftmode"/>
			<arg value="${doc.intro.tex}"/>
920 921 922
		</pdflatex>

		<pdflatex dir="${build.doc.intro}">
923
			<arg value="${doc.intro.tex}"/>
924
		</pdflatex>
925 926 927
	</target>


928
	<target name="build-jdoc" depends="-init,compile">
929
		<mkdir dir="${out.doc.jdoc}"/>
930 931
		<javadoc destdir="${out.doc.jdoc}"
			access="public" author="true" version="true" use="true"
932 933
			noDeprecated="false" nodeprecatedlist="false"
			noIndex="false" splitIndex="true" noNavbar="false" noTree="false"
934
			overview="${src.doc.jdoc}/overview.html"
935
			source="1.8"
936 937
		>
			<sourcepath>
938
				<pathelement path="${src.disl}"/>
939 940 941 942
			</sourcepath>

			<sourcefiles>
				<fileset dir="${src.disl}">
943 944 945 946 947 948
					<include name="**/DiSL.java"/>
					<include name="**/Reflection.java"/>
					<include name="**/Scope.java"/>
					<include name="**/ScopeMatcher.java"/>
					<include name="**/Shadow.java"/>
					<include name="**/Transformer.java"/>
949 950
				</fileset>
				<fileset dir="${src.disl.bypass}">
951
					<include name="**/DynamicBypass.java"/>
952
				</fileset>
953 954

				<fileset dir="${src.shvm}">
955 956 957 958 959
					<include name="**/RemoteAnalysis.java"/>
					<include name="**/ShadowObject.java"/>
					<include name="**/ShadowString.java"/>
					<include name="**/ShadowThread.java"/>
					<include name="**/ShadowClass.java"/>
960 961
				</fileset>
				<fileset dir="${src.shvm.dispatch}">
962
					<include name="**/REDispatch.java"/>
963
				</fileset>
964 965 966
			</sourcefiles>

			<classpath>
967 968 969 970
				<path refid="disl.bypass.classpath"/>
				<path refid="shvm.classpath"/>
				<path refid="util.classpath"/>
				<path refid="asm.classpath"/>
971 972
			</classpath>

973 974 975 976 977 978 979
			<package name="ch.usi.dag.disl.annotation"/>
			<package name="ch.usi.dag.disl.marker"/>
			<package name="ch.usi.dag.disl.guardcontext"/>
			<package name="ch.usi.dag.disl.staticcontext"/>
			<package name="ch.usi.dag.disl.dynamiccontext"/>
			<package name="ch.usi.dag.disl.classcontext"/>
			<package name="ch.usi.dag.disl.processorcontext"/>
980 981 982 983 984 985
		</javadoc>
	</target>


	<!-- Miscellaneous -->

986
	<target name="copy-libs" depends="-init">
987 988
		<copy todir="${out.lib}" flatten="true">
			<path refid="asm.classpath"/>
989
			<path refid="protobuf.classpath"/>
990
		</copy>
991 992 993 994
	</target>


	<target name="package-src">
995
		<zip destfile="${out.src}/src.zip">
996 997 998 999 1000
			<zipfileset dir="${src.disl}"/>
			<zipfileset dir="${src.disl.bypass}/dynamic"/>
			<zipfileset dir="${src.shvm}"/>
			<zipfileset dir="${src.shvm.dispatch}"/>
			<zipfileset dir="${src.util}"/>
1001 1002 1003 1004 1005 1006
		</zip>
	</target>


	<!-- Releases -->

1007
	<target name="release-bin" depends="build,package-src">
1008
		<!-- Collect the artifacts from output and source directories. -->
1009
		<zip destfile="${release.bin.zip}">
1010
			<zipfileset prefix="${disl.prefix}/${dist.bin}" dir="${src.bin}" fileMode="755"/>
1011
			<zipfileset prefix="${disl.prefix}/${dist.lib}" dir="${out.lib}"/>
1012 1013
			<zipfileset prefix="${disl.prefix}/${dist.doc.intro}" file="${doc.intro.pdf}"/>
			<zipfileset prefix="${disl.prefix}/${dist.doc.jdoc}" dir="${out.doc.jdoc}"/>
1014
			<zipfileset prefix="${disl.prefix}/${dist.examples}" dir="${src.examples}">
1015
				<exclude name="build-override.properties"/>
1016
			</zipfileset>
1017
			<zipfileset prefix="${disl.prefix}/${dist.src}" dir="${out.src}"/>
1018
			<zipfileset prefix="${disl.prefix}" dir=".">
1019 1020 1021
				<include name="COPYING"/>
				<include name="README"/>
				<include name="USER_ERRORS"/>
1022
			</zipfileset>
1023 1024
		</zip>

1025
		<!-- Convert the zip archive to bzip2 compressed tar. -->
1026
		<tar destfile="${release.bin.tar}" compression="bzip2">
1027
			<zipfileset src="${release.bin.zip}"/>
1028 1029 1030 1031
		</tar>
	</target>


1032
	<target name="release-src" depends="clean-examples">
1033 1034
		<zip destfile="${release.src.zip}">
			<zipfileset prefix="disl-${disl.version}" dir=".">
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
				<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.*"/>
1053
			</zipfileset>
1054 1055
			<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"/>
1056 1057 1058
		</zip>

		<tar destfile="${release.src.tar}" compression="bzip2">
1059
			<zipfileset src="${release.src.zip}"/>
1060 1061 1062 1063 1064
		</tar>
	</target>


	<!-- Summary targets -->
1065 1066 1067
        <target name="properties" depends="-determine-lib-names">
		<echoproperties/>
        </target>
1068

1069
	<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."/>
1070

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

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

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

1077
	<target name="release" depends="doc,release-bin,release-src" description="Produces DiSL source and binary release archives."/>
1078 1079 1080 1081


	<!-- Clean up -->

1082
	<target name="clean-examples">
1083
		<ant dir="${src.examples}" target="clean"/>
1084 1085 1086
	</target>


1087
	<target name="clean" description="Removes all files produced during the build.">
1088
		<exec executable="make" dir="${src.disl.agent}">
1089
			<arg value="cleanall"/>
1090 1091 1092
		</exec>

		<exec executable="make" dir="${src.shvm.agent}">
1093
			<arg value="cleanall"/>
1094
		</exec>
1095

1096
		<delete dir="${out.dir}"/>
1097 1098
	</target>

1099 1100 1101 1102 1103

	<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}"/>
1104
		<delete dir="${lib.devel.dir}"/>
1105 1106
	</target>

1107
</project>