blob: 8988598c204c50c49ba70c40b2c2ebfb844cb10e [file] [log] [blame]
Nicolas Catania081e65d2009-10-09 21:17:27 -07001;;; android-common.el --- Common functions/variables to dev Android in Emacs.
2;;
3;; Copyright (C) 2009 The Android Open Source Project
4;;
5;; Licensed under the Apache License, Version 2.0 (the "License");
6;; you may not use this file except in compliance with the License.
7;; You may obtain a copy of the License at
8;;
9;; http://www.apache.org/licenses/LICENSE-2.0
10;;
11;; Unless required by applicable law or agreed to in writing, software
12;; distributed under the License is distributed on an "AS IS" BASIS,
13;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14;; See the License for the specific language governing permissions and
15;; limitations under the License.
Nicolas Cataniaf94bca22009-10-06 10:45:44 -070016
Nicolas Catania081e65d2009-10-09 21:17:27 -070017;;; Commentary:
18;;
19;; Variables to customize and common functions for the Android build
20;; support in Emacs.
21;; There should be no interactive function in this module.
22;;
23;; You need to have a proper buildspec.mk file in your root directory
24;; for this module to work (see $TOP/build/buildspec.mk.default).
25;; If the path the product's files/image uses an a product alias, you
26;; need to add a mapping in `android-product-alias-map'. For instance
27;; if TARGET_PRODUCT is foo but the build directory is out/target/product/bar,
28;; you need to add a mapping Target:foo -> Alias:bar
29;;
30
31;;; Code:
Nicolas Cataniaf94bca22009-10-06 10:45:44 -070032
33(defgroup android nil
34 "Support for android development in Emacs."
35 :group 'tools)
36
37;;;###autoload
38(defcustom android-compilation-jobs 2
39 "Number of jobs used to do a compilation (-j option of make)."
40 :type 'integer
41 :group 'android)
42
Nicolas Catania081e65d2009-10-09 21:17:27 -070043;;;###autoload
44(defcustom android-product-alias-map nil
45 "Alist between product targets (declared in buildspec.mk) and actual
46 product build directory used by `android-product'.
47
48For instance if TARGET_PRODUCT is 'foo' but the build directory
49 is 'out/target/product/bar', you need to add a mapping Target:foo -> Alias:bar."
50 :type '(repeat (list (string :tag "Target")
51 (string :tag "Alias")))
52 :group 'android)
53
Nicolas Cataniaf94bca22009-10-06 10:45:44 -070054(defun android-find-build-tree-root ()
55 "Ascend the current path until the root of the android build tree is found.
56Similarly to the shell functions in envsetup.sh, for the root both ./Makefile
57and ./build/core/envsetup.mk are exiting files.
Nicolas Catania081e65d2009-10-09 21:17:27 -070058Return the root of the build tree. Signal an error if not found."
Nicolas Cataniaf94bca22009-10-06 10:45:44 -070059 (let ((default-directory default-directory))
60 (while (and (> (length default-directory) 2)
Nicolas Catania081e65d2009-10-09 21:17:27 -070061 (not (file-exists-p (concat default-directory
62 "Makefile")))
63 (not (file-exists-p (concat default-directory
64 "build/core/envsetup.mk"))))
Nicolas Cataniaf94bca22009-10-06 10:45:44 -070065 (setq default-directory
66 (substring default-directory 0
67 (string-match "[^/]+/$" default-directory))))
68 (if (> (length default-directory) 2)
69 default-directory
Nicolas Catania081e65d2009-10-09 21:17:27 -070070 (error "Not in a valid android tree"))))
Nicolas Cataniaf94bca22009-10-06 10:45:44 -070071
72(defun android-project-p ()
Nicolas Catania081e65d2009-10-09 21:17:27 -070073 "Return nil if not in an android build tree."
Nicolas Cataniaf94bca22009-10-06 10:45:44 -070074 (condition-case nil
75 (android-find-build-tree-root)
76 (error nil)))
77
Nicolas Catania081e65d2009-10-09 21:17:27 -070078(defun android-host ()
79 "Return the <system>-<arch> string (e.g linux-x86).
80Only linux and darwin on x86 architectures are supported."
81 (or (string-match "x86" system-configuration)
82 (string-match "i386" system-configuration)
83 (error "Unknown arch"))
84 (or (and (string-match "darwin" system-configuration) "darwin-x86")
85 (and (string-match "linux" system-configuration) "linux-x86")
86 (error "Unknown system")))
87
88(defun android-product ()
89 "Return the product built according to the buildspec.mk.
90You must have buildspec.mk file in the top directory.
91
92Additional product aliases can be listed in `android-product-alias-map'
93if the product actually built is different from the one listed
94in buildspec.mk"
95 (save-excursion
96 (let* ((buildspec (concat (android-find-build-tree-root) "buildspec.mk"))
97 (product (with-current-buffer (find-file-noselect buildspec)
98 (goto-char (point-min))
99 (search-forward "TARGET_PRODUCT:=")
100 (buffer-substring-no-properties (point)
101 (scan-sexps (point) 1))))
102 (alias (assoc product android-product-alias-map)))
103 ; Post processing, adjust the names.
104 (if (not alias)
105 product
106 (nth 1 alias)))))
107
108(defun android-product-path ()
109 "Return the full path to the product directory.
110
111Additional product aliases can be added in `android-product-alias-map'
112if the product actually built is different from the one listed
113in buildspec.mk"
114 (let ((path (concat (android-find-build-tree-root) "out/target/product/"
115 (android-product))))
116 (when (not (file-exists-p path))
117 (error (format "%s does not exist. If product %s maps to another one,
118add an entry to android-product-map." path (android-product))))
119 path))
120
121(defun android-find-host-bin (binary)
122 "Return the full path to the host BINARY.
123Binaries don't depend on the device, just on the host type.
124Try first to locate BINARY in the out/host tree. Fallback using
125the shell exec PATH setup."
126 (if (android-project-p)
127 (let ((path (concat (android-find-build-tree-root) "out/host/"
128 (android-host) "/bin/" binary)))
129 (if (file-exists-p path)
130 path
131 (error (concat binary " is missing."))))
132 (executable-find binary)))
133
134(defun android-adb ()
135 "Return the path to the adb executable.
136If not in the build tree use the PATH env variable."
137 (android-find-host-bin "adb"))
138
139(defun android-fastboot ()
140 "Return the path to the fastboot executable.
141If not in the build tree use the PATH env variable."
142 ; For fastboot -p is the name of the product, *not* the full path to
143 ; its directory like adb requests sometimes.
144 (concat (android-find-host-bin "fastboot") " -p " (android-product)))
145
146(defun android-adb-command (command &optional product)
147 "Execute 'adb COMMAND'.
148If the optional PRODUCT is not nil, -p (android-product-path) is used
149when adb is invoked."
150 (if product
151 (shell-command (concat (android-adb) " -p " (android-product-path)
152 " " command))
153 (shell-command (concat (android-adb) " " command))))
154
155(defun android-adb-shell-command (command)
156 "Execute 'adb shell COMMAND'."
157 (android-adb-command (concat " shell " command)))
158
Nicolas Cataniaf94bca22009-10-06 10:45:44 -0700159(provide 'android-common)
Nicolas Catania081e65d2009-10-09 21:17:27 -0700160
161;;; android-common.el ends here