Line data Source code
1 : /* C++ wrapper around libiberty's pex API.
2 : Copyright (C) 2025-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #ifndef GCC_PEX_H
22 : #define GCC_PEX_H
23 :
24 : struct file_wrapper
25 : {
26 : enum class ownership { owned, borrowed };
27 :
28 98 : file_wrapper (FILE *file, enum ownership ownership)
29 98 : : m_file (file),
30 98 : m_ownership (ownership)
31 : {
32 : }
33 98 : ~file_wrapper ()
34 : {
35 98 : if (m_ownership == ownership::owned)
36 : {
37 0 : gcc_assert (m_file);
38 0 : fclose (m_file);
39 : }
40 98 : }
41 :
42 : std::unique_ptr<std::vector<char>>
43 : read_all ();
44 :
45 : FILE *m_file;
46 : enum ownership m_ownership;
47 : };
48 :
49 : // RAII wrapper around pex_obj
50 :
51 : struct pex
52 : {
53 49 : pex (int flags, const char *pname, const char *tempbase)
54 49 : : m_obj (pex_init (flags, pname, tempbase))
55 : {
56 : }
57 :
58 49 : ~pex ()
59 : {
60 49 : pex_free (m_obj);
61 49 : }
62 :
63 : const char *
64 : run (int flags, const char *executable, char * const *argv,
65 : const char *outname, const char *errname, int *err)
66 : {
67 : return pex_run (m_obj, flags, executable, argv, outname, errname, err);
68 : }
69 :
70 : const char *
71 : run (int flags, const char *executable, const std::vector<std::string> &args,
72 : const char *outname, const char *errname, int *err);
73 :
74 : file_wrapper
75 49 : input_file (int flags, const char *in_name)
76 : {
77 49 : return file_wrapper (pex_input_file (m_obj, flags, in_name),
78 : /* closed on first call to pex_run. */
79 49 : file_wrapper::ownership::borrowed);
80 : }
81 :
82 : file_wrapper
83 : input_pipe (bool binary = true)
84 : {
85 : return file_wrapper (pex_input_pipe (m_obj, binary),
86 : /* closed on first call to pex_run. */
87 : file_wrapper::ownership::borrowed);
88 : }
89 :
90 : file_wrapper
91 49 : read_output (bool binary = true)
92 : {
93 49 : return file_wrapper (pex_read_output (m_obj, binary),
94 49 : file_wrapper::ownership::borrowed);
95 : }
96 :
97 : pex_obj *m_obj;
98 : };
99 :
100 : #endif /* GCC_PEX_H */
|