GCC Middle and Back End API Reference
pex.h
Go to the documentation of this file.
1/* C++ wrapper around libiberty's pex API.
2 Copyright (C) 2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
25{
26 enum class ownership { owned, borrowed };
27
28 file_wrapper (FILE *file, enum ownership ownership)
29 : m_file (file),
31 {
32 }
34 {
36 {
38 fclose (m_file);
39 }
40 }
41
42 std::unique_ptr<std::vector<char>>
43 read_all ();
44
45 FILE *m_file;
47};
48
49// RAII wrapper around pex_obj
50
51struct pex
52{
53 pex (int flags, const char *pname, const char *tempbase)
54 : m_obj (pex_init (flags, pname, tempbase))
55 {
56 }
57
59 {
60 pex_free (m_obj);
61 }
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
75 input_file (int flags, const char *in_name)
76 {
77 return file_wrapper (pex_input_file (m_obj, flags, in_name),
78 /* closed on first call to pex_run. */
80 }
81
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. */
88 }
89
91 read_output (bool binary = true)
92 {
93 return file_wrapper (pex_read_output (m_obj, binary),
95 }
96
97 pex_obj *m_obj;
98};
99
100#endif /* GCC_PEX_H */
Definition pex.h:25
file_wrapper(FILE *file, enum ownership ownership)
Definition pex.h:28
FILE * m_file
Definition pex.h:45
enum ownership m_ownership
Definition pex.h:46
ownership
Definition pex.h:26
@ borrowed
Definition pex.h:26
@ owned
Definition pex.h:26
std::unique_ptr< std::vector< char > > read_all()
Definition pex.cc:63
~file_wrapper()
Definition pex.h:33
file_wrapper input_file(int flags, const char *in_name)
Definition pex.h:75
~pex()
Definition pex.h:58
pex_obj * m_obj
Definition pex.h:97
file_wrapper input_pipe(bool binary=true)
Definition pex.h:83
file_wrapper read_output(bool binary=true)
Definition pex.h:91
pex(int flags, const char *pname, const char *tempbase)
Definition pex.h:53
const char * run(int flags, const char *executable, char *const *argv, const char *outname, const char *errname, int *err)
Definition pex.h:64
#define gcc_assert(EXPR)
Definition system.h:814