TestJavaCompilation.gmk revision 2318:8d337fd6333e
1# 2# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4# 5# This code is free software; you can redistribute it and/or modify it 6# under the terms of the GNU General Public License version 2 only, as 7# published by the Free Software Foundation. Oracle designates this 8# particular file as subject to the "Classpath" exception as provided 9# by Oracle in the LICENSE file that accompanied this code. 10# 11# This code is distributed in the hope that it will be useful, but WITHOUT 12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14# version 2 for more details (a copy is included in the LICENSE file that 15# accompanied this code). 16# 17# You should have received a copy of the GNU General Public License version 18# 2 along with this work; if not, write to the Free Software Foundation, 19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20# 21# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22# or visit www.oracle.com if you need additional information or have any 23# questions. 24# 25 26default: all 27 28include $(SPEC) 29include MakeBase.gmk 30include JarArchive.gmk 31include JavaCompilation.gmk 32 33THIS_FILE := $(SRC_ROOT)/test/make/TestJavaCompilation.gmk 34DEPS := $(THIS_FILE) \ 35 $(SRC_ROOT)/make/common/MakeBase.gmk \ 36 $(SRC_ROOT)/make/common/JavaCompilation.gmk \ 37 # 38 39OUTPUT_DIR := $(TESTMAKE_OUTPUTDIR)/java-compilation 40 41################################################################################ 42# Test: jar1 43# Creates a simple jar file and unzips it to verify that the files have not 44# changed. 45 46JAR1_SRC_ROOT := $(OUTPUT_DIR)/jar1src 47JAR1_UNZIP := $(OUTPUT_DIR)/jar1unzip 48JAR1_FILE := $(OUTPUT_DIR)/jar1.jar 49JAR1_MANIFEST := $(OUTPUT_DIR)/jar1_manifest 50 51clean-jar1: 52 $(RM) -r $(OUTPUT_DIR)/_jar1* $(OUTPUT_DIR)/jar1* 53 54$(JAR1_MANIFEST): | $(OUTPUT_DIR)/_jar1_created 55 $(ECHO) "Test-Attribute: value" > $(JAR1_MANIFEST) 56 57$(OUTPUT_DIR)/_jar1_created: $(DEPS) 58 $(RM) -r $(JAR1_SRC_ROOT) 59 $(RM) $(JAR1_FILE) 60 $(RM) -r $(JAR1_UNZIP) 61 $(MKDIR) -p $(JAR1_SRC_ROOT) 62 $(MKDIR) -p $(JAR1_SRC_ROOT)/dir1 63 $(MKDIR) -p $(JAR1_SRC_ROOT)/dir2 64 $(MKDIR) -p $(JAR1_SRC_ROOT)/META-INF 65 $(TOUCH) $(JAR1_SRC_ROOT)/dir1/file1.class 66 $(TOUCH) $(JAR1_SRC_ROOT)/dir2/file2.class 67 $(TOUCH) $(JAR1_SRC_ROOT)/META-INF/metafile 68 $(TOUCH) $@ 69 70$(eval $(call SetupJarArchive, BUILD_JAR1, \ 71 DEPENDENCIES := $(OUTPUT_DIR)/_jar1_created, \ 72 SRCS := $(JAR1_SRC_ROOT), \ 73 MANIFEST := $(JAR1_MANIFEST), \ 74 JAR := $(JAR1_FILE), \ 75)) 76 77$(OUTPUT_DIR)/_jar1_verified: $(BUILD_JAR1) 78 $(RM) -r $(JAR1_UNZIP) 79 $(MKDIR) -p $(JAR1_UNZIP) 80 $(CD) $(JAR1_UNZIP) && $(UNZIP) $(JAR1_FILE) $(LOG_DEBUG) 81 $(DIFF) -r $(JAR1_SRC_ROOT)/dir1 $(JAR1_UNZIP)/dir1 82 $(DIFF) -r $(JAR1_SRC_ROOT)/dir2 $(JAR1_UNZIP)/dir2 83 $(DIFF) -r $(JAR1_SRC_ROOT)/META-INF/metafile $(JAR1_UNZIP)/META-INF/metafile 84 if [ "`$(GREP) 'Test-Attribute: value' $(JAR1_UNZIP)/META-INF/MANIFEST.MF`" = "" ]; then \ 85 $(ECHO) "Could not find Test-Attribute in manifest of $(JAR1_FILE)"; \ 86 exit 1; \ 87 fi 88 $(TOUCH) $@ 89 90create-jar2: $(OUTPUT_DIR)/_jar1_verified 91TEST_TARGETS += $(OUTPUT_DIR)/_jar1_verified 92 93# Change a source file and call this makefile again to force the jar to be 94# updated. 95$(OUTPUT_DIR)/_jar1_updated: $(OUTPUT_DIR)/_jar1_verified 96 $(ECHO) updated > $(JAR1_SRC_ROOT)/dir1/file1.class 97 $(ECHO) updated > $(JAR1_SRC_ROOT)/META-INF/metafile 98 $(TOUCH) $(OUTPUT_DIR)/_jar1_created 99 +$(MAKE) -f $(THIS_FILE) $(OUTPUT_DIR)/_jar1_verified 100 $(TOUCH) $@ 101 102update-jar1: $(OUTPUT_DIR)_jar1_updated 103 104# Change the manifest file and call this makefile again to force the jar 105# to be updated 106$(OUTPUT_DIR)/_jar1_updated_manifest: $(OUTPUT_DIR)/_jar1_updated 107 $(ECHO) "Test-Attribute: foobar" > $(JAR1_MANIFEST) 108 +$(MAKE) -f $(THIS_FILE) $(BUILD_JAR1) 109 $(RM) -r $(JAR1_UNZIP) 110 $(MKDIR) -p $(JAR1_UNZIP) 111 $(CD) $(JAR1_UNZIP) && $(UNZIP) $(JAR1_FILE) $(LOG_DEBUG) 112 if [ "`$(GREP) 'Test-Attribute: foobar' $(JAR1_UNZIP)/META-INF/MANIFEST.MF`" = "" ]; then \ 113 $(ECHO) "Could not find Test-Attribute in manifest of $(JAR1_FILE)"; \ 114 exit 1; \ 115 fi 116 $(TOUCH) $@ 117 118update-jar1-manifest: $(OUTPUT_DIR)/_jar1_updated_manifest 119 120TEST_TARGETS += $(OUTPUT_DIR)/_jar1_updated $(OUTPUT_DIR)/_jar1_updated_manifest 121 122.PHONY: clean-jar1 create-jar1 update-jar1 update-jar1-manifest 123 124################################################################################ 125# Test: jar2 126# Creates a jar file based on 2 source roots 127 128JAR2_SRC_ROOT1 := $(OUTPUT_DIR)/jar2src1 129JAR2_SRC_ROOT2 := $(OUTPUT_DIR)/jar2src2 130JAR2_UNZIP := $(OUTPUT_DIR)/jar2unzip 131JAR2_FILE := $(OUTPUT_DIR)/jar2.jar 132 133clean-jar2: 134 $(RM) -r $(OUTPUT_DIR)/_jar2* $(OUTPUT_DIR)/jar2* 135 136$(OUTPUT_DIR)/_jar2_created: $(DEPS) 137 $(RM) -r $(JAR2_SRC_ROOT1) 138 $(RM) -r $(JAR2_SRC_ROOT2) 139 $(RM) $(JAR2_FILE) 140 $(RM) -r $(JAR2_UNZIP) 141 $(MKDIR) -p $(JAR2_SRC_ROOT1)/dir1 142 $(MKDIR) -p $(JAR2_SRC_ROOT2)/dir2 143 $(TOUCH) $(JAR2_SRC_ROOT1)/dir1/file1.class 144 $(TOUCH) $(JAR2_SRC_ROOT2)/dir2/file2.class 145 $(TOUCH) $@ 146 147$(eval $(call SetupJarArchive, BUILD_JAR2, \ 148 DEPENDENCIES := $(OUTPUT_DIR)/_jar2_created, \ 149 SRCS := $(JAR2_SRC_ROOT1) $(JAR2_SRC_ROOT2), \ 150 JAR := $(JAR2_FILE), \ 151)) 152 153$(OUTPUT_DIR)/_jar2_verified: $(BUILD_JAR2) 154 $(RM) -r $(JAR2_UNZIP) 155 $(MKDIR) -p $(JAR2_UNZIP) 156 $(CD) $(JAR2_UNZIP) && $(UNZIP) $(JAR2_FILE) $(LOG_DEBUG) 157 $(DIFF) -r $(JAR2_SRC_ROOT1)/dir1 $(JAR2_UNZIP)/dir1 158 $(DIFF) -r $(JAR2_SRC_ROOT2)/dir2 $(JAR2_UNZIP)/dir2 159 $(TOUCH) $@ 160 161create-jar2: $(OUTPUT_DIR)/_jar2_verified 162TEST_TARGETS += $(OUTPUT_DIR)/_jar2_verified 163 164$(OUTPUT_DIR)/_jar2_updated: $(OUTPUT_DIR)/_jar2_verified 165 $(ECHO) updated > $(JAR2_SRC_ROOT1)/dir1/file1.class 166 $(TOUCH) $(OUTPUT_DIR)/_jar2_created 167 +$(MAKE) -f $(THIS_FILE) $(OUTPUT_DIR)/_jar2_verified 168 $(TOUCH) $@ 169 170update-jar2: $(OUTPUT_DIR)/_jar2_updated 171TEST_TARGETS += $(OUTPUT_DIR)/_jar2_updated 172 173.PHONY: clean-jar2 create-jar2 update-jar2 174 175################################################################################ 176# Test: jar3 177# Creates a jar file based on 2 source roots with an extra file 178 179JAR3_SRC_ROOT1 := $(OUTPUT_DIR)/jar3src1 180JAR3_SRC_ROOT2 := $(OUTPUT_DIR)/jar3src2 181JAR3_UNZIP := $(OUTPUT_DIR)/jar3unzip 182JAR3_FILE := $(OUTPUT_DIR)/jar3.jar 183 184clean-jar3: 185 $(RM) -r $(OUTPUT_DIR)/_jar3* $(OUTPUT_DIR)/jar3* 186 187$(OUTPUT_DIR)/_jar3_created: $(DEPS) 188 $(RM) -r $(JAR3_SRC_ROOT1) 189 $(RM) -r $(JAR3_SRC_ROOT2) 190 $(RM) $(JAR3_FILE) 191 $(RM) -r $(JAR3_UNZIP) 192 $(MKDIR) -p $(JAR3_SRC_ROOT1)/dir1 193 $(MKDIR) -p $(JAR3_SRC_ROOT2)/dir2 194 $(TOUCH) $(JAR3_SRC_ROOT1)/dir1/file1\$$foo.class 195 $(TOUCH) $(JAR3_SRC_ROOT2)/dir2/file2.class 196 $(TOUCH) $(JAR3_SRC_ROOT2)/extra-file 197 $(TOUCH) $(JAR3_SRC_ROOT2)/extra-file-abs 198 $(TOUCH) $(JAR3_SRC_ROOT2)/dir2/file\$$foo.dollar 199 $(TOUCH) $@ 200 201$(eval $(call SetupJarArchive, BUILD_JAR3, \ 202 DEPENDENCIES := $(OUTPUT_DIR)/_jar3_created, \ 203 SRCS := $(JAR3_SRC_ROOT1) $(JAR3_SRC_ROOT2), \ 204 EXTRA_FILES := extra-file \ 205 dir2/file$$foo.dollar \ 206 $(JAR3_SRC_ROOT2)/extra-file-abs, \ 207 EXCLUDE_FILES := dir1/file1$$foo.class, \ 208 JAR := $(JAR3_FILE), \ 209)) 210 211$(OUTPUT_DIR)/_jar3_verified: $(BUILD_JAR3) 212 $(RM) -r $(JAR3_UNZIP) 213 $(MKDIR) -p $(JAR3_UNZIP) 214 $(CD) $(JAR3_UNZIP) && $(UNZIP) $(JAR3_FILE) $(LOG_DEBUG) 215 if [ -d "$(JAR3_UNZIP)/dir1" ]; then \ 216 echo Should not be included $(JAR3_UNZIP)/dir1; \ 217 exit 1; \ 218 fi 219 $(DIFF) -r $(JAR3_SRC_ROOT2)/dir2 $(JAR3_UNZIP)/dir2 220 $(DIFF) -r $(JAR3_SRC_ROOT2)/extra-file $(JAR3_UNZIP)/extra-file 221 $(TOUCH) $@ 222 223create-jar3: $(OUTPUT_DIR)/_jar3_verified 224TEST_TARGETS += $(OUTPUT_DIR)/_jar3_verified 225 226$(OUTPUT_DIR)/_jar3_updated: $(OUTPUT_DIR)/_jar3_verified 227 $(ECHO) updated > $(JAR3_SRC_ROOT2)/extra-file 228 $(TOUCH) $(OUTPUT_DIR)/_jar3_created 229 +$(MAKE) -f $(THIS_FILE) $(OUTPUT_DIR)/_jar3_verified 230 $(TOUCH) $@ 231 232update-jar3: $(OUTPUT_DIR)/_jar3_updated 233TEST_TARGETS += $(OUTPUT_DIR)/_jar3_updated 234 235.PHONY: clean-jar3 create-jar3 update-jar3 236 237################################################################################ 238# Test SetupJavaCompilation overrides of java files 239 240$(eval $(call SetupJavaCompiler,BOOT_JAVAC, \ 241 JAVAC := $(JAVAC), \ 242 DISABLE_SJAVAC := true, \ 243)) 244 245JAVA_SRC_ROOT1 := $(OUTPUT_DIR)/javaroot1 246JAVA_SRC_ROOT2 := $(OUTPUT_DIR)/javaroot2 247 248# Since this makefile calls itself a number of times, protect this macro from 249# being executed more than once. 250# Param 1 - File name 251# Param 2 - Package name 252# Param 3 - Class name 253# Param 4 - Message 254CreateJavaSrc = \ 255 $(if $(wildcard $1),,$(shell \ 256 $(MKDIR) -p $(dir $1); \ 257 $(ECHO) "package $2;" > $1; \ 258 $(ECHO) "public class $3 {" >> $1; \ 259 $(ECHO) " public static void main(String[] args) {" >> $1; \ 260 $(ECHO) " System.out.print(\"$4\");" >> $1; \ 261 $(ECHO) " }" >> $1; \ 262 $(ECHO) "}" >> $1; \ 263 )) 264 265# Since this makefile calls itself a number of times, protect this macro from 266# being executed more than once. 267# Param 1 - File name 268# Param 2 - Message 269CreateTextFile = \ 270 $(if $(wildcard $1),,$(shell \ 271 $(MKDIR) -p $(dir $1); \ 272 $(PRINTF) '$2' > $1; \ 273 )) 274 275$(call CreateJavaSrc,$(JAVA_SRC_ROOT1)/a/A.java,a,A,javaroot1) 276$(call CreateJavaSrc,$(JAVA_SRC_ROOT2)/a/A.java,a,A,javaroot2) 277$(call CreateTextFile,$(JAVA_SRC_ROOT1)/a/b.txt,javaroot1\n) 278$(call CreateTextFile,$(JAVA_SRC_ROOT2)/a/b.txt,javaroot2\n) 279$(call CreateTextFile,$(JAVA_SRC_ROOT1)/a/c.properties,#javaroot1\nname=value1\n) 280$(call CreateTextFile,$(JAVA_SRC_ROOT2)/a/c.properties,#javaroot2\nname=value2\n) 281 282# Due to a bug in gnu make 3.81, need to add the src roots with trailing slash, 283# otherwise $(wildcard ) will not find the directories and the sanity check in 284# SetupJavaCompilation will fail. 285$(eval $(call SetupJavaCompilation, BUILD_ROOT1_FIRST, \ 286 SETUP := BOOT_JAVAC, \ 287 SRC := $(JAVA_SRC_ROOT1)/ $(JAVA_SRC_ROOT2)/, \ 288 COPY := .txt .java, \ 289 CLEAN := .properties, \ 290 BIN := $(OUTPUT_DIR)/root1first/, \ 291)) 292 293$(BUILD_ROOT1_FIRST): 294 295verify-root1-first: $(BUILD_ROOT1_FIRST) 296 $(JAVA_SMALL) -cp $(OUTPUT_DIR)/root1first a.A > $(OUTPUT_DIR)/root1first.output 297 if [ "`$(CAT) $(OUTPUT_DIR)/root1first.output`" != "javaroot1" ]; then \ 298 $(ECHO) "The wrong class was compiled. Expected >javaroot1<"; \ 299 $(ECHO) "Got >`$(CAT) $(OUTPUT_DIR)/root1first.output`<"; \ 300 false; \ 301 fi 302 if [ "`$(CAT) $(OUTPUT_DIR)/root1first/a/b.txt`" != "javaroot1" ]; then \ 303 $(ECHO) "The wrong file was copied. Expected >javaroot1<"; \ 304 $(ECHO) "Got >`$(CAT) $(OUTPUT_DIR)/root1first/a/b.txt`<"; \ 305 false; \ 306 fi 307 if [ ! -e "$(OUTPUT_DIR)/root1first/a/A.java" ]; then \ 308 $(ECHO) "Missed copying $(OUTPUT_DIR)/root1first/a/A.java"; \ 309 false; \ 310 fi 311 if [ "`$(CAT) $(OUTPUT_DIR)/root1first/a/c.properties`" != "name=value1" ]; then \ 312 $(ECHO) "The wrong file was cleaned. Expected >name=value1<"; \ 313 $(ECHO) "Got >`$(CAT) $(OUTPUT_DIR)/root1first/a/c.properties`<"; \ 314 false; \ 315 fi 316 317$(eval $(call SetupJavaCompilation, BUILD_ROOT2_FIRST, \ 318 SETUP := BOOT_JAVAC, \ 319 SRC := $(JAVA_SRC_ROOT2)/ $(JAVA_SRC_ROOT1)/, \ 320 COPY := .txt, \ 321 CLEAN := .properties, \ 322 BIN := $(OUTPUT_DIR)/root2first/, \ 323)) 324 325$(BUILD_ROOT2_FIRST): 326 327verify-root2-first: $(BUILD_ROOT2_FIRST) 328 $(JAVA_SMALL) -cp $(OUTPUT_DIR)/root2first a.A > $(OUTPUT_DIR)/root2first.output 329 if [ "`$(CAT) $(OUTPUT_DIR)/root2first.output`" != "javaroot2" ]; then \ 330 $(ECHO) "The wrong class was compiled. Expected >javaroot2<"; \ 331 $(ECHO) "Got >`$(CAT) $(OUTPUT_DIR)/root2first.output`<"; \ 332 false; \ 333 fi 334 if [ "`$(CAT) $(OUTPUT_DIR)/root2first/a/b.txt`" != "javaroot2" ]; then \ 335 $(ECHO) "The wrong file was cleaned. Expected >javaroot2<"; \ 336 $(ECHO) "Got >`$(CAT) $(OUTPUT_DIR)/root2first/a/b.txt`<"; \ 337 false; \ 338 fi 339 if [ "`$(CAT) $(OUTPUT_DIR)/root2first/a/c.properties`" != "name=value2" ]; then \ 340 $(ECHO) "The wrong file was cleaned. Expected >name=value2<"; \ 341 $(ECHO) "Got >`$(CAT) $(OUTPUT_DIR)/root2first/a/c.properties`<"; \ 342 false; \ 343 fi 344 345TEST_TARGETS += verify-root1-first verify-root2-first 346 347.PHONY: verify-root1-first verify-root2-first 348 349################################################################################ 350 351all: $(TEST_TARGETS) 352 353.PHONY: default all 354