|
| 1 | +BOOL_ATTRS = [ |
| 2 | + "public", |
| 3 | + "protected", |
| 4 | + "private", |
| 5 | + "nohelp", |
| 6 | + "nonavbar", |
| 7 | + "use", |
| 8 | + "warn", |
| 9 | + "quiet", |
| 10 | + "breakiterator", |
| 11 | + "version", |
| 12 | + "author", |
| 13 | + "docfilessubdirs", |
| 14 | + "splitindex", |
| 15 | + "nocomment", |
| 16 | + "nodeprecated", |
| 17 | + "nosince", |
| 18 | + "notimestamp", |
| 19 | + "nodeprecatedlist", |
| 20 | + "notree", |
| 21 | + "noindex", |
| 22 | + "serialwarn", |
| 23 | + "linksource", |
| 24 | + "keywords", |
| 25 | +] |
| 26 | + |
| 27 | +#default = "com.inxar.doclet1.standard.Standard", |
| 28 | + |
| 29 | +STRING_ATTRS = [ |
| 30 | + "doclet", |
| 31 | + "tagletpath", |
| 32 | + "source", |
| 33 | + "charset", |
| 34 | + "locale", |
| 35 | + "encoding", |
| 36 | + "link", |
| 37 | + "windowtitle", |
| 38 | + "doctitle", |
| 39 | + "header", |
| 40 | + "footer", |
| 41 | + "top", |
| 42 | + "bottom", |
| 43 | + "sourcetab", |
| 44 | +] |
| 45 | + |
| 46 | +STRING_LIST_ATTRS = [ |
| 47 | + "J_opts", |
| 48 | + "X_opts", |
| 49 | + "taglet", |
| 50 | + "tag", |
| 51 | +] |
| 52 | + |
| 53 | +DICT_STRING_STRING_ATTRS = [ |
| 54 | + "linkoffline", |
| 55 | +] |
| 56 | + |
| 57 | +PATH_LIST_ATTRS = [ |
| 58 | + "exclude", |
| 59 | + "subpackages", |
| 60 | + "extdirs", |
| 61 | + "excludedocfilessubdir", |
| 62 | + "noqualifier", |
| 63 | +] |
| 64 | + |
| 65 | +SINGLE_FILE_PATH_ATTRS = [ |
| 66 | + "overview", |
| 67 | + "helpfile", |
| 68 | + "stylesheetfile", |
| 69 | +] |
| 70 | + |
| 71 | +def _mk_attrs(): |
| 72 | + # |
| 73 | + # Non-biolerplate attrs; either more complex definitions or have |
| 74 | + # special handling in the rule implementation. |
| 75 | + # |
| 76 | + attrs = { |
| 77 | + "sourcepaths": attr.string_list(mandatory = False), |
| 78 | + "groups": attr.string_dict(), |
| 79 | + "java_tool": attr.label( |
| 80 | + default = Label("@local_jdk//:java"), |
| 81 | + single_file = True, |
| 82 | + executable = True, |
| 83 | + ), |
| 84 | + "tools_jar": attr.label( |
| 85 | + default = Label("//java:tools_jar"), |
| 86 | + single_file = True, |
| 87 | + ), |
| 88 | + "doclet_deps": attr.label_list( |
| 89 | + default = [Label("//src/com/inxar/doclet1")], |
| 90 | + providers = ["java"], |
| 91 | + ), |
| 92 | + "taglet_deps": attr.label_list( |
| 93 | + providers = ["java"], |
| 94 | + ), |
| 95 | + "deps": attr.label_list( |
| 96 | + mandatory = True, |
| 97 | + providers = ["java"], |
| 98 | + ), |
| 99 | + "srcs": attr.label_list( |
| 100 | + allow_files = FileType([".java"]), |
| 101 | + ), |
| 102 | + "packages": attr.string_list(), |
| 103 | + "outdir": attr.string(), |
| 104 | + } |
| 105 | + |
| 106 | + # |
| 107 | + # Biolerplate attrs |
| 108 | + # |
| 109 | + for name in BOOL_ATTRS: |
| 110 | + attrs[name] = attr.bool() |
| 111 | + for name in STRING_ATTRS: |
| 112 | + attrs[name] = attr.string() |
| 113 | + for name in STRING_LIST_ATTRS: |
| 114 | + attrs[name] = attr.string_list() |
| 115 | + for name in PATH_LIST_ATTRS: |
| 116 | + attrs[name] = attr.string_list() |
| 117 | + for name in SINGLE_FILE_PATH_ATTRS: |
| 118 | + attrs[name] = attr.label(single_file = True, allow_files = True) |
| 119 | + |
| 120 | + # Boilerplate overrides |
| 121 | + attrs["quiet"] = attr.bool(default = True) |
| 122 | + |
| 123 | + return attrs |
| 124 | + |
| 125 | + |
| 126 | +def _get_java_dep_jars(deps): |
| 127 | + jars = [] |
| 128 | + for dep in deps: |
| 129 | + for jar in dep.files: |
| 130 | + jars.append(jar) |
| 131 | + for jar in dep.java.transitive_deps: |
| 132 | + if not jar.basename.endswith("-ijar.jar"): |
| 133 | + jars.append(jar) |
| 134 | + return jars |
| 135 | + |
| 136 | + |
| 137 | +def _get_javadoc_tool(java_tool): |
| 138 | + javadoc = java_tool.dirname + "/javadoc" |
| 139 | + if java_tool.basename.endswith(".exe"): |
| 140 | + javadoc += ".exe" |
| 141 | + return javadoc |
| 142 | + |
| 143 | + |
| 144 | +def _get_output_index_html(ctx, outdir): |
| 145 | + filename = "index.html" |
| 146 | + if outdir: |
| 147 | + filename = outdir + "/" + filename |
| 148 | + return ctx.new_file(filename) |
| 149 | + |
| 150 | + |
| 151 | +def _pathlist(files): |
| 152 | + return ":".join([file.path for file in files]) |
| 153 | + |
| 154 | + |
| 155 | +def _java_apidoc_impl(ctx): |
| 156 | + |
| 157 | + # |
| 158 | + # Preparation of inputs and dependencies. |
| 159 | + # |
| 160 | + java = ctx.executable.java_tool |
| 161 | + javadoc = _get_javadoc_tool(java) |
| 162 | + jars = _get_java_dep_jars(ctx.attr.deps) |
| 163 | + jars.append(ctx.file.tools_jar) |
| 164 | + doclet_jars = _get_java_dep_jars(ctx.attr.doclet_deps) |
| 165 | + taglet_jars = _get_java_dep_jars(ctx.attr.taglet_deps) |
| 166 | + srcfiles = ctx.files.srcs |
| 167 | + index_html = _get_output_index_html(ctx, ctx.attr.outdir) |
| 168 | + all_jars = jars + doclet_jars + taglet_jars |
| 169 | + inputs = [java] + all_jars + srcfiles |
| 170 | + |
| 171 | + # |
| 172 | + # Command generation. |
| 173 | + # |
| 174 | + cmds = [] |
| 175 | + if ctx.attr.outdir: |
| 176 | + cmds += ["mkdir -p %s" % index_html.dirname] |
| 177 | + |
| 178 | + args = [javadoc] |
| 179 | + args += ["-d", index_html.dirname] |
| 180 | + args += ["-classpath", _pathlist(jars)] |
| 181 | + if doclet_jars: args += ["-docletpath", _pathlist(doclet_jars)] |
| 182 | + if taglet_jars: args += ["-tagletpath", _pathlist(taglet_jars)] |
| 183 | + if ctx.attr.sourcepaths: args += ["-sourcepath", ":".join(ctx.attr.sourcepaths)] |
| 184 | + |
| 185 | + if not ctx.attr.warn: |
| 186 | + args += ["-Xdoclint:none"] |
| 187 | + for k, v in ctx.attr.groups.items(): |
| 188 | + args += ["-group", k, v] |
| 189 | + |
| 190 | + # |
| 191 | + # Boilerplate args |
| 192 | + # |
| 193 | + for key in BOOL_ATTRS: |
| 194 | + if getattr(ctx.attr, key): |
| 195 | + args += ["-" + key] |
| 196 | + for key in STRING_ATTRS: |
| 197 | + val = getattr(ctx.attr, key) |
| 198 | + if val: args += ["-" + key, "'%s'" % val] |
| 199 | + for key in PATH_LIST_ATTRS: |
| 200 | + vals = getattr(ctx.attr, key) |
| 201 | + if vals: args += ["-" + key, ":".join(vals)] |
| 202 | + for key in STRING_LIST_ATTRS: |
| 203 | + for val in getattr(ctx.attr, key): |
| 204 | + args += ["-" + key, val] |
| 205 | + for key in SINGLE_FILE_PATH_ATTRS: |
| 206 | + if hasattr(ctx.file, key): |
| 207 | + file = getattr(ctx.file, key) |
| 208 | + if file: |
| 209 | + args += ["-" + key, file.path] |
| 210 | + inputs.append(file) |
| 211 | + |
| 212 | + args += ctx.attr.packages |
| 213 | + args += [file.path for file in srcfiles] |
| 214 | + |
| 215 | + javadoc_cmd = " ".join(args) |
| 216 | + if not ctx.attr.quiet: |
| 217 | + print("javadoc: %s" % "\n".join(args)) |
| 218 | + cmds.append(javadoc_cmd) |
| 219 | + |
| 220 | + #print("cmd: %s" % " && ".join(cmds)) |
| 221 | + |
| 222 | + ctx.action( |
| 223 | + progress_message = "JavaDoc", |
| 224 | + command = " && ".join(cmds), |
| 225 | + inputs = inputs, |
| 226 | + outputs = [index_html], |
| 227 | + ) |
| 228 | + |
| 229 | + return struct( |
| 230 | + files = set([index_html]), |
| 231 | + ) |
| 232 | + |
| 233 | +java_apidoc = rule( |
| 234 | + implementation = _java_apidoc_impl, |
| 235 | + attrs = _mk_attrs(), |
| 236 | + output_to_genfiles = True, |
| 237 | +) |
0 commit comments