Skip to content

Commit 5314a7c

Browse files
authored
Allow .cxx and .cc extension in compile (#2273)
* Allow .cxx and .cc extensions * Added integration test
1 parent 304d48c commit 5314a7c

File tree

7 files changed

+81
-10
lines changed

7 files changed

+81
-10
lines changed

arduino/globals/globals.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ var (
3535
".hpp": empty,
3636
".hh": empty,
3737
".cpp": empty,
38+
".cxx": empty,
39+
".cc": empty,
3840
".S": empty,
3941
".adoc": empty,
4042
".md": empty,
@@ -43,11 +45,15 @@ var (
4345
".ipp": empty,
4446
}
4547

46-
// SourceFilesValidExtensions lists valid extensions for source files (no headers)
47-
SourceFilesValidExtensions = map[string]struct{}{
48-
".c": empty,
49-
".cpp": empty,
50-
".S": empty,
48+
// SourceFilesValidExtensions lists valid extensions for source files (no headers).
49+
// If a platform do not provide a compile recipe for a specific file extension, this
50+
// map provides the equivalent extension to use as a fallback.
51+
SourceFilesValidExtensions = map[string]string{
52+
".c": "",
53+
".cpp": "",
54+
".cxx": ".cpp",
55+
".cc": ".cpp",
56+
".S": "",
5157
}
5258

5359
// HeaderFilesValidExtensions lists valid extensions for header files

docs/platform-specification.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,15 @@ These properties can be overwritten respectively with `--keys-keychain`, `--sign
172172
#### Recipes to compile source code
173173

174174
We said that the Arduino development software determines a list of files to compile. Each file can be source code
175-
written in C (.c files), C++ (.cpp files) or Assembly (.S files). Every language is compiled using its respective
176-
**recipe**:
175+
written in C (.c files), C++ (.cpp/.cxx/.cc files) or Assembly (.S files). Every language is compiled using its
176+
respective **recipe**:
177177

178-
- `recipe.c.o.pattern`: for C files
179-
- `recipe.cpp.o.pattern`: for CPP files
180-
- `recipe.S.o.pattern`: for Assembly files
178+
- `recipe.c.o.pattern`: for C files (.c)
179+
- `recipe.cpp.o.pattern`: for CPP files (.cpp/.cxx/.cc)
180+
- `recipe.S.o.pattern`: for Assembly files (.S)
181+
182+
(an optional `recipe.cxx.o.pattern` and `recipe.cc.o.pattern` may be provided, if `.cxx` or `.cc` needs special
183+
handling, but it's not required and we do not recommend it)
181184

182185
The recipes can be built concatenating the following automatically generated properties (for each file compiled):
183186

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package compile_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/internal/integrationtest"
22+
"github.com/arduino/go-paths-helper"
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
func TestCompileSketchWithCxxOrCc(t *testing.T) {
27+
// See: https://github.com/arduino/arduino-cli/issues/1149
28+
29+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
30+
defer env.CleanUp()
31+
32+
// Run update-index with our test index
33+
_, _, err := cli.Run("core", "install", "arduino:avr@1.8.5")
34+
require.NoError(t, err)
35+
36+
// Prepare sketchbook and sketch
37+
sketch, err := paths.New("testdata", "sketch_with_cxx_cc").Abs()
38+
require.NoError(t, err)
39+
40+
_, _, err = cli.Run("compile", "-v", "-b", "arduino:avr:uno", sketch.String())
41+
require.NoError(t, err)
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
void func_in_cc();
2+
void func_in_cxx();
3+
4+
void setup() {
5+
func_in_cc();
6+
func_in_cxx();
7+
}
8+
9+
void loop() {
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void func_in_cc() {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void func_in_cxx() {
2+
3+
}

legacy/builder/builder_utils/utils.go

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func compileFiles(ctx *types.Context, sourcePath *paths.Path, recurse bool, buil
109109
queue := make(chan *paths.Path)
110110
job := func(source *paths.Path) {
111111
recipe := fmt.Sprintf("recipe%s.o.pattern", source.Ext())
112+
if !buildProperties.ContainsKey(recipe) {
113+
recipe = fmt.Sprintf("recipe%s.o.pattern", globals.SourceFilesValidExtensions[source.Ext()])
114+
}
112115
objectFile, err := compileFileWithRecipe(ctx, sourcePath, source, buildPath, buildProperties, includes, recipe)
113116
if err != nil {
114117
errorsMux.Lock()

0 commit comments

Comments
 (0)