toolchain.eclass: fix ada build with lto
[gentoo.git] / eclass / java-osgi.eclass
1 # Copyright 2007-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: java-osgi.eclass
5 # @MAINTAINER:
6 # java@gentoo.org
7 # @AUTHOR:
8 # Java maintainers (java@gentoo.org)
9 # @BLURB: Java OSGi eclass
10 # @DESCRIPTION:
11 # This eclass provides functionality which is used by packages that need to be
12 # OSGi compliant. This means that the generated jars will have special headers
13 # in their manifests. Currently this is used only by Eclipse-3.3 - later we
14 # could extend this so that Gentoo Java system would be fully OSGi compliant.
15
16 inherit java-utils-2
17
18 # @ECLASS-VARIABLE: _OSGI_T
19 # @INTERNAL
20 # @DESCRIPTION:
21 # We define _OSGI_T so that it does not contain a slash at the end.
22 # According to Paludis guys, there is currently a proposal for EAPIs that
23 # would require all variables to end with a slash.
24 _OSGI_T="${T/%\//}"
25
26 # must get Diego to commit something like this to portability.eclass
27 _canonicalise() {
28         if type -p realpath > /dev/null; then
29                 realpath "${@}"
30         elif type -p readlink > /dev/null; then
31                 readlink -f "${@}"
32         else
33                 # can't die, subshell
34                 eerror "No readlink nor realpath found, cannot canonicalise"
35         fi
36 }
37
38 # @FUNCTION: _java-osgi_plugin
39 # @USAGE: <plugin name>
40 # @INTERNAL
41 # @DESCRIPTION:
42 # This is an internal function, not to be called directly.
43 #
44 # @CODE
45 #       _java-osgi_plugin "JSch"
46 # @CODE
47 #
48 # @param $1 - bundle name
49 _java-osgi_plugin() {
50         # We hardcode Gentoo as the vendor name
51
52         cat > "${_OSGI_T}/tmp_jar/plugin.properties" <<-EOF
53         bundleName="${1}"
54         vendorName="Gentoo"
55         EOF
56 }
57
58 # @FUNCTION: _java-osgi_makejar
59 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
60 # @INTERNAL
61 # @DESCRIPTION:
62 # This is an internal function, not to be called directly.
63 #
64 # @CODE
65 #       _java-osgi_makejar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
66 # @CODE
67 #
68 # @param $1 - name of jar to repackage with OSGi
69 # @param $2 - bundle symbolic name
70 # @param $3 - bundle name
71 # @param $4 - export-package header
72 _java-osgi_makejar() {
73         debug-print-function ${FUNCNAME} "$@"
74
75         (( ${#} < 4 )) && die "Four arguments are needed for _java-osgi_makejar()"
76
77         local absoluteJarPath="$(_canonicalise ${1})"
78         local jarName="$(basename ${1})"
79
80         mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
81         [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
82
83         cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
84                  || die "Unable to uncompress correctly the original jar"
85
86         cat > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" <<-EOF
87         Manifest-Version: 1.0
88         Bundle-ManifestVersion: 2
89         Bundle-Name: %bundleName
90         Bundle-Vendor: %vendorName
91         Bundle-Localization: plugin
92         Bundle-SymbolicName: ${2}
93         Bundle-Version: ${PV}
94         Export-Package: ${4}
95         EOF
96
97         _java-osgi_plugin "${3}"
98
99         jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
100                 -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
101         rm -rf "${_OSGI_T}/tmp_jar"
102 }
103
104 # @FUNCTION: @java-osgi_dojar
105 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
106 # @DESCRIPTION:
107 # Rewrites a jar, and produce an OSGi compliant jar from arguments given on the command line.
108 # The arguments given correspond to the minimal set of headers
109 # that must be present on a Manifest file of an OSGi package.
110 # If you need more headers, you should use the *-fromfile functions below,
111 # that create the Manifest from a file.
112 # It will call java-pkg_dojar at the end.
113 #
114 # @CODE
115 #       java-osgi_dojar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
116 # @CODE
117 #
118 # @param $1 - name of jar to repackage with OSGi
119 # @param $2 - bundle symbolic name
120 # @param $3 - bundle name
121 # @param $4 - export-package-header
122 java-osgi_dojar() {
123         debug-print-function ${FUNCNAME} "$@"
124         local jarName="$(basename ${1})"
125         _java-osgi_makejar "$@"
126         java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"
127 }
128
129 # @FUNCTION: java-osgi_newjar
130 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
131 # @DESCRIPTION:
132 # Rewrites a jar, and produce an OSGi compliant jar.
133 # The arguments given correspond to the minimal set of headers
134 # that must be present on a Manifest file of an OSGi package.
135 # If you need more headers, you should use the *-fromfile functions below,
136 # that create the Manifest from a file.
137 # It will call java-pkg_newjar at the end.
138 #
139 # @CODE
140 #       java-osgi_newjar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
141 # @CODE
142 #
143 # @param $1 - name of jar to repackage with OSGi
144 # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
145 # @param $3 - bundle symbolic name
146 # @param $4 - bundle name
147 # @param $5 - export-package header
148 java-osgi_newjar() {
149         debug-print-function ${FUNCNAME} "$@"
150         local jarName="$(basename $1)"
151
152         if (( ${#} > 4 )); then
153                 _java-osgi_makejar "${1}" "${3}" "${4}" "${5}"
154                 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
155         else
156                 _java-osgi_makejar "$@"
157                 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
158         fi
159 }
160
161 # @FUNCTION:_java-osgi_makejar-fromfile
162 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
163 # @INTERNAL
164 # @DESCRIPTION:
165 # This is an internal function, not to be called directly.
166 #
167 # @CODE
168 #       _java-osgi_makejar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "JSch" 1
169 # @CODE
170 #
171 # @param $1 - name of jar to repackage with OSGi
172 # @param $2 - path to the Manifest file
173 # @param $3 - bundle name
174 # @param $4 - automatic version rewriting (0 or 1)
175 _java-osgi_makejar-fromfile() {
176         debug-print-function ${FUNCNAME} "$@"
177
178         ((${#} < 4)) && die "Four arguments are needed for _java-osgi_makejar-fromfile()"
179
180         local absoluteJarPath="$(_canonicalise ${1})"
181         local jarName="$(basename ${1})"
182
183         mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
184         [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
185
186         cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
187                 || die "Unable to uncompress correctly the original jar"
188
189         [[ -e "${2}" ]] || die "Manifest file ${2} not found"
190
191         # We automatically change the version if automatic version rewriting is on
192
193         if (( ${4} )); then
194                 cat "${2}" | sed "s/Bundle-Version:.*/Bundle-Version: ${PV}/" > \
195                         "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
196         else
197                 cat "${2}" > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
198         fi
199
200         _java-osgi_plugin "${3}"
201
202         jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
203                 -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
204         rm -rf "${_OSGI_T}/tmp_jar"
205 }
206
207 # @FUNCTION: java-osgi_newjar-fromfile
208 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
209 # @DESCRIPTION:
210 # This function produces an OSGi compliant jar from a given manifest file.
211 # The Manifest Bundle-Version header will be replaced by the current version
212 # of the package, unless the --no-auto-version option is given.
213 # It will call java-pkg_newjar at the end.
214 #
215 # @CODE
216 #       java-osgi_newjar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
217 # @CODE
218 #
219 # @param $opt
220 #       --no-auto-version - This option disables automatic rewriting of the
221 #               version in the Manifest file
222 #
223 # @param $1 - name of jar to repackage with OSGi
224 # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
225 # @param $3 - path to the Manifest file
226 # @param $4 - bundle name
227 java-osgi_newjar-fromfile() {
228         debug-print-function ${FUNCNAME} "$@"
229         local versionRewriting=1
230
231         if [[ "${1}" == "--no-auto-version" ]]; then
232                 versionRewriting=0
233                 shift
234         fi
235         local jarName="$(basename ${1})"
236
237         if (( ${#} > 3 )); then
238                 _java-osgi_makejar-fromfile "${1}" "${3}" "${4}" "${versionRewriting}"
239                 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
240         else
241                 _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
242                 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
243         fi
244 }
245
246 # @FUNCTION: java-osgi_dojar-fromfile
247 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name>
248 # @DESCRIPTION:
249 # This function produces an OSGi compliant jar from a given manifestfile.
250 # The Manifest Bundle-Version header will be replaced by the current version
251 # of the package, unless the --no-auto-version option is given.
252 # It will call java-pkg_dojar at the end.
253 #
254 # @CODE
255 #       java-osgi_dojar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
256 # @CODE
257 #
258 # @param $opt
259 #       --no-auto-version - This option disables automatic rewriting of the
260 #               version in the Manifest file
261 #
262 # @param $1 - name of jar to repackage with OSGi
263 # @param $2 - path to the Manifest file
264 # @param $3 - bundle name
265 java-osgi_dojar-fromfile() {
266         debug-print-function ${FUNCNAME} "$@"
267         local versionRewriting=1
268
269         if [[ "${1}" == "--no-auto-version" ]]; then
270                 versionRewriting=0
271                 shift
272         fi
273         local jarName="$(basename ${1})"
274
275         _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
276         java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"
277 }