Buck: Create javadoc from source archive

javadoc accepts source archive and we need to create one anyway. So
instead of trying to use the sources in the tree and guess the root
project directory, just use the source archive. We extact the archive
in temporary directory to make javadoc work.

Change-Id: Ib605f6cdab4742a23789da8fbc9c963c83e5b6d9
diff --git a/gerrit-acceptance-framework/BUCK b/gerrit-acceptance-framework/BUCK
index f960010..4bce902 100644
--- a/gerrit-acceptance-framework/BUCK
+++ b/gerrit-acceptance-framework/BUCK
@@ -77,7 +77,7 @@
   name = 'acceptance-framework-javadoc',
   title = 'Gerrit Acceptance Test Framework Documentation',
   pkgs = [' com.google.gerrit.acceptance'],
-  paths = ['src/test/java'],
+  source_jar = ':acceptance-framework-src',
   srcs = SRCS,
   deps = DEPS + PROVIDED + [
     '//lib:guava',
diff --git a/gerrit-extension-api/BUCK b/gerrit-extension-api/BUCK
index dda1290..7a8b671 100644
--- a/gerrit-extension-api/BUCK
+++ b/gerrit-extension-api/BUCK
@@ -71,7 +71,7 @@
   name = 'extension-api-javadoc',
   title = 'Gerrit Review Extension API Documentation',
   pkgs = ['com.google.gerrit.extensions'],
-  paths = ['src/main/java'],
+  source_jar = ':extension-api-src',
   srcs = SRCS,
   deps = [
     '//lib:guava',
diff --git a/gerrit-plugin-api/BUCK b/gerrit-plugin-api/BUCK
index 70886d5..7e38726 100644
--- a/gerrit-plugin-api/BUCK
+++ b/gerrit-plugin-api/BUCK
@@ -83,7 +83,7 @@
   name = 'plugin-api-javadoc',
   title = 'Gerrit Review Plugin API Documentation',
   pkgs = ['com.google.gerrit'],
-  paths = [n for n in SRCS],
+  source_jar = ':plugin-api-src',
   srcs = glob([n + '**/*.java' for n in SRCS]),
   deps = [
     ':plugin-api',
@@ -92,5 +92,4 @@
     '//lib/bouncycastle:bcpkix',
   ],
   visibility = ['PUBLIC'],
-  do_it_wrong = True,
 )
diff --git a/gerrit-plugin-gwtui/BUCK b/gerrit-plugin-gwtui/BUCK
index 2ee0e19..575ebfc 100644
--- a/gerrit-plugin-gwtui/BUCK
+++ b/gerrit-plugin-gwtui/BUCK
@@ -1,8 +1,4 @@
-COMMON = ['gerrit-gwtui-common/src/main/java/']
-GWTEXPUI = ['gerrit-gwtexpui/src/main/java/']
-SRC = 'src/main/java/com/google/gerrit/'
-SRCS = glob([SRC + '**/*.java'])
-
+SRCS = glob(['src/main/java/com/google/gerrit/**/*.java'])
 DEPS = ['//lib/gwt:user']
 
 java_binary(
@@ -50,7 +46,7 @@
     'com.google.gwtexpui.safehtml',
     'com.google.gwtexpui.user',
   ],
-  paths = COMMON + GWTEXPUI,
+  source_jar = ':gwtui-api-src',
   srcs = SRCS,
   deps = DEPS + [
     '//lib:gwtjsonrpc',
@@ -61,5 +57,4 @@
     '//gerrit-reviewdb:client',
   ],
   visibility = ['PUBLIC'],
-  do_it_wrong = True,
 )
diff --git a/tools/java_doc.defs b/tools/java_doc.defs
index 65865bb..b8778bb 100644
--- a/tools/java_doc.defs
+++ b/tools/java_doc.defs
@@ -2,20 +2,18 @@
     name,
     title,
     pkgs,
-    paths,
+    source_jar,
     srcs = [],
     deps = [],
     visibility = [],
-    do_it_wrong = False,
   ):
-  if do_it_wrong:
-    sourcepath = paths
-  else:
-    sourcepath = ['$SRCDIR/' + n for n in paths]
+  # TODO(davido): Actually we shouldn't need to extract the source
+  # archive, javadoc should just work with provided archive.
   genrule(
     name = name,
     cmd = ' '.join([
-      'while ! test -f .buckconfig; do cd ..; done;',
+      'mkdir $TMP/sourcepath &&',
+      'unzip $(location %s) -d $TMP/sourcepath &&' % source_jar,
       'javadoc',
       '-quiet',
       '-protected',
@@ -26,8 +24,7 @@
       '-link http://docs.oracle.com/javase/7/docs/api',
       '-subpackages ',
       ':'.join(pkgs),
-      '-sourcepath ',
-      ':'.join(sourcepath),
+      '-sourcepath $TMP/sourcepath',
       ' -classpath ',
       ':'.join(['$(classpath %s)' % n for n in deps]),
       '-d $TMP',
@@ -35,4 +32,4 @@
     srcs = srcs,
     out = name + '.jar',
     visibility = visibility,
-)
+  )