Macroprogramming—the technique of specifying the behavior of the system, as opposed to the constituent nodes—provides application developers with high level abstractions that alleviate the programming burden in developing wireless sensor network (WSN) applications. However, as the semantic gap macroprogramming abstractions and node-level code is considerably wider than in traditional programming, converting the high level specification to running code is a daunting process, and a major hurdle to the acceptance of macroprogramming. In this paper, we propose a general compilation framework for a data-driven macroprogramming language that allows for plugging in different modules implementing various stages of compilation. We also demonstrate an actual instantiation of our framework by showing an end-to-end solution for compiling macroprograms. Our compiler provides the final code to be deployed on real nodes as well as an estimate of the costs the running system will incur, e.g., in...