Nanopb: Header And Source Are Not Generated Before Building The Target.

by ADMIN 72 views

Introduction

In this article, we will discuss a critical issue with the nanopb library in the Zephyr project. The problem arises when the auto-generated header and source files are not generated before building the target. This issue can be particularly frustrating when working on a driver that relies on nanopb. In this article, we will delve into the details of the bug, provide a step-by-step guide on how to reproduce the issue, and discuss the expected behavior and impact of this problem.

Describe the Bug

The issue at hand is that the auto-generated header and source files for nanopb are not generated before building the target. This is evident from the CMakeLists.txt file provided, which includes the following lines:

zephyr_nanopb_sources(app
  my_driver_proto.pb
)

zephyr_library_sources(
  my_driver.c
  ${ZEPHYR_BASE}/build/zephyr/drivers/my_driver/my_driver_proto.pb.c
)

As we can see, the zephyr_nanopb_sources function is used to generate the source file my_driver_proto.pb.c, but this file is not generated before building the target. If we comment out this source file, the build proceeds, and the files get generated.

To Reproduce

To reproduce this issue, you will need to follow these steps:

  1. Create a driver that uses nanopb. You can use the instructions provided in the Zephyr documentation to create a new driver.
  2. Create a sample that uses/enables this driver. You can use the samples provided in the Zephyr documentation to create a new sample.
  3. Build the sample using the CMakeLists.txt file provided above.
  4. Observe that the auto-generated header and source files are not generated before building the target.

Expected Behavior

The expected behavior is that the auto-generated header and source files should be generated before building the target. This is because the zephyr_nanopb_sources function is used to generate the source file my_driver_proto.pb.c, and this file is required for the build process.

Impact

The impact of this issue is significant, as it prevents the build process from completing successfully. This can be particularly frustrating when working on a driver that relies on nanopb. As a workaround, you can add the auto-generated files manually, but this is not a sustainable solution.

Logs and Console Output

The logs and console output for this issue are as follows:

zephyr/cmake/modules/extensions.cmake:515 (target_sources):
  Cannot find source file:
    /home/zephyr/drivers/my_driver/my_driver_proto.pb.c

This error message indicates that the build process is unable to find the source file my_driver_proto.pb.c, which is required for the build process.

Environment

The environment in which this issue was encountered is as follows:

  • OS: Linux
  • Toolchain: Zephyr SDK

Conclusion

In conclusion, the issue with nanopb in the Zephyr project is a critical problem that prevents the build process from completing successfully. The auto-generated header and source files are not generated before building the target, which can be particularly frustrating when working on a driver that relies on nanopb. By following the steps outlined in this article, you can reproduce this issue and understand the expected behavior and impact of this problem.

Workaround

As a workaround, you can add the auto-generated files manually. However, this is not a sustainable solution and should be avoided whenever possible.

Future Development

In the future, it would be beneficial to address this issue and ensure that the auto-generated header and source files are generated before building the target. This can be achieved by modifying the zephyr_nanopb_sources function to generate the source file before building the target.

Code Snippets

Here are some code snippets that demonstrate the issue:

if (CONFIG_MY_DRIVER)

list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_BASE}/modules/nanopb)
include(nanopb)

zephyr_nanopb_sources(app
  my_driver_proto.pb
)

zephyr_library_sources(
  my_driver.c
  ${ZEPHYR_BASE}/build/zephyr/drivers/my_driver/my_driver_proto.pb.c
)

zephyr_include_directories(
  ./
  ${ZEPHYR_BASE}/build/zephyr/drivers/my_driver/
)

endif()
zephyr/cmake/modules/extensions.cmake:515 (target_sources):
  Cannot find source file:
    /home/zephyr/drivers/my_driver/my_driver_proto.pb.c

References

Introduction

In our previous article, we discussed a critical issue with the nanopb library in the Zephyr project. The problem arises when the auto-generated header and source files are not generated before building the target. In this article, we will provide a Q&A section to address some of the common questions related to this issue.

Q: What is the cause of this issue?

A: The cause of this issue is due to the way the zephyr_nanopb_sources function is used in the CMakeLists.txt file. The function is used to generate the source file, but it is not executed before building the target.

Q: How can I reproduce this issue?

A: To reproduce this issue, you will need to follow these steps:

  1. Create a driver that uses nanopb. You can use the instructions provided in the Zephyr documentation to create a new driver.
  2. Create a sample that uses/enables this driver. You can use the samples provided in the Zephyr documentation to create a new sample.
  3. Build the sample using the CMakeLists.txt file provided above.
  4. Observe that the auto-generated header and source files are not generated before building the target.

Q: What is the expected behavior?

A: The expected behavior is that the auto-generated header and source files should be generated before building the target. This is because the zephyr_nanopb_sources function is used to generate the source file, and this file is required for the build process.

Q: What is the impact of this issue?

A: The impact of this issue is significant, as it prevents the build process from completing successfully. This can be particularly frustrating when working on a driver that relies on nanopb. As a workaround, you can add the auto-generated files manually, but this is not a sustainable solution.

Q: How can I fix this issue?

A: To fix this issue, you will need to modify the zephyr_nanopb_sources function to generate the source file before building the target. This can be achieved by adding a dependency on the zephyr_nanopb_sources function before building the target.

Q: What are the system requirements for this issue?

A: The system requirements for this issue are:

  • OS: Linux
  • Toolchain: Zephyr SDK

Q: Can I use a workaround to fix this issue?

A: Yes, you can use a workaround to fix this issue. One possible workaround is to add the auto-generated files manually. However, this is not a sustainable solution and should be avoided whenever possible.

Q: Are there any known solutions to this issue?

A: Yes, there are known solutions to this issue. One possible solution is to modify the zephyr_nanopb_sources function to generate the source file before building the target. This can be achieved by adding a dependency on the zephyr_nanopb_sources function before building the target.

Q: Can I get help from the Zephyr community?

A: Yes, you can get help from the Zephyr community. The Zephyr community is active and responsive, and you can reach out to them for help with this issue.

Conclusion

In conclusion, the issue with nanopb in the Zephyr project is a critical problem that prevents the build process from completing successfully. The auto-generated header and source files are not generated before building the target, which can be particularly frustrating when working on a driver that relies on nanopb. By following the steps outlined in this article, you can reproduce this issue and understand the expected behavior and impact of this problem.

References