aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/markdown/Reference-manual.md11
-rw-r--r--docs/markdown/snippets/pathdivision.md15
-rw-r--r--mesonbuild/interpreter.py2
-rw-r--r--mesonbuild/interpreterbase.py13
-rw-r--r--test cases/common/116 pathjoin/meson.build7
5 files changed, 43 insertions, 5 deletions
diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md
index c9fe25b..f43f1f6 100644
--- a/docs/markdown/Reference-manual.md
+++ b/docs/markdown/Reference-manual.md
@@ -1005,7 +1005,7 @@ the jar with `java -jar file.jar`.
### join_paths()
``` meson
- string join_paths(string1, string2, ...)
+string join_paths(string1, string2, ...)
```
Joins the given strings into a file system path segment. For example
@@ -1015,6 +1015,15 @@ dropped. That means that `join_paths('foo', '/bar')` returns `/bar`.
*Added 0.36.0*
+Since 0.49.0 using the`/` operator on strings is equivalent to calling
+`join_paths`.
+
+```meson
+# res1 and res2 will have identical values
+res1 = join_paths(foo, bar)
+res2 = foo / bar
+```
+
### library()
``` meson
diff --git a/docs/markdown/snippets/pathdivision.md b/docs/markdown/snippets/pathdivision.md
new file mode 100644
index 0000000..6da6005
--- /dev/null
+++ b/docs/markdown/snippets/pathdivision.md
@@ -0,0 +1,15 @@
+## Joining paths with /
+
+Joining two paths has traditionally been done with the `join_paths` function.
+
+```meson
+joined = join_paths('foo', 'bar')
+```
+
+Now you can use the simpler notation using the `/` operator.
+
+```meson
+joined = 'foo' / 'bar'
+```
+
+This only works for strings.
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
index 7e5ad27..2b27694 100644
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -3810,7 +3810,7 @@ different subdirectory.
@stringArgs
@noKwargs
def func_join_paths(self, node, args, kwargs):
- return os.path.join(*args).replace('\\', '/')
+ return self.join_path_strings(args)
def run(self):
super().run()
diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py
index aee1c87..66e9dd6 100644
--- a/mesonbuild/interpreterbase.py
+++ b/mesonbuild/interpreterbase.py
@@ -382,6 +382,9 @@ class InterpreterBase:
me.file = environment.build_filename
raise me
+ def join_path_strings(self, args):
+ return os.path.join(*args).replace('\\', '/')
+
def parse_project(self):
"""
Parses project() and initializes languages, compilers etc. Do this
@@ -628,9 +631,13 @@ The result of this is undefined and will become a hard error in a future Meson r
raise InvalidCode('Multiplication works only with integers.')
return l * r
elif cur.operation == 'div':
- if not isinstance(l, int) or not isinstance(r, int):
- raise InvalidCode('Division works only with integers.')
- return l // r
+ if isinstance(l, str) and isinstance(r, str):
+ return self.join_path_strings((l, r))
+ if isinstance(l, int) and isinstance(r, int):
+ if r == 0:
+ raise InvalidCode('Division by zero.')
+ return l // r
+ raise InvalidCode('Division works only with strings or integers.')
elif cur.operation == 'mod':
if not isinstance(l, int) or not isinstance(r, int):
raise InvalidCode('Modulo works only with integers.')
diff --git a/test cases/common/116 pathjoin/meson.build b/test cases/common/116 pathjoin/meson.build
index 751ca68..227ef67 100644
--- a/test cases/common/116 pathjoin/meson.build
+++ b/test cases/common/116 pathjoin/meson.build
@@ -15,3 +15,10 @@ assert(join_paths(['foo', 'bar', 'baz']) == 'foo/bar/baz', 'Path joining is brok
assert(join_paths(['/foo', 'bar']) == '/foo/bar', 'Path joining is broken')
assert(join_paths(['foo', '/bar']) == '/bar', 'Absolute path joining is broken')
assert(join_paths(['/foo', '/bar']) == '/bar', 'Absolute path joining is broken')
+
+# Division operator should do the same as join_paths
+assert('foo' / 'bar' == 'foo/bar', 'Path division is broken')
+assert('foo' /'bar' /'baz' == 'foo/bar/baz', 'Path division is broken')
+assert('/foo' / 'bar' == '/foo/bar', 'Path division is broken')
+assert('foo' / '/bar' == '/bar', 'Absolute path division is broken')
+assert('/foo' / '/bar' == '/bar', 'Absolute path division is broken')